src/hbservers/hbthemeserver/hbthemeserverapplication.cpp
changeset 34 ed14f46c0e55
parent 7 923ff622b8b9
equal deleted inserted replaced
31:7516d6d86cf5 34:ed14f46c0e55
    30 #include <QWindowsStyle>
    30 #include <QWindowsStyle>
    31 #include <QLibrary>
    31 #include <QLibrary>
    32 #include <QDebug>
    32 #include <QDebug>
    33 #include <QDir>
    33 #include <QDir>
    34 
    34 
    35 #if defined (Q_OS_SYMBIAN)
       
    36 #include "hbthemecommon_symbian_p.h"
    35 #include "hbthemecommon_symbian_p.h"
    37 #include <eikenv.h>
    36 #include <eikenv.h>
    38 #include <apgwgnam.h>
    37 #include <apgwgnam.h>
    39 #endif
       
    40 
    38 
    41 static const QLatin1String APP_NAME("HbThemeServer");
    39 static const QLatin1String APP_NAME("HbThemeServer");
    42 static const QLatin1String RESOURCE_LIB_NAME("HbCore");
    40 static const QLatin1String RESOURCE_LIB_NAME("HbCore");
    43 static const QLatin1String TEST_RESOURCE_LIB_NAME("HbTestResources");
    41 static const QLatin1String TEST_RESOURCE_LIB_NAME("HbTestResources");
    44 static const QLatin1String WIN32_DEBUG_SUFFIX("d");
       
    45 static const QLatin1String MAC_DEBUG_SUFFIX("_debug");
       
    46 static const QLatin1String STOP_MESSAGE("stop");
       
    47 
       
    48 bool HbThemeServerApplication::Options::help = false;
       
    49 bool HbThemeServerApplication::Options::start = false;
       
    50 bool HbThemeServerApplication::Options::stop = false;
       
    51 bool HbThemeServerApplication::Options::persistent = false;
       
    52 QString HbThemeServerApplication::Options::error;
       
    53 
    42 
    54 HbThemeServerApplication::HbThemeServerApplication(int &argc, char *argv[]) :
    43 HbThemeServerApplication::HbThemeServerApplication(int &argc, char *argv[]) :
    55     QtSingleApplication(argc, argv), server(0)
    44     QtSingleApplication(argc, argv), server(0)
    56 {
    45 {
    57     setApplicationName(APP_NAME);
    46     setApplicationName(APP_NAME);
    58 
    47 
    59 #ifdef QT_DEBUG
    48 #ifdef QT_DEBUG
    60     //temporary solution until Hb specific style is ready
    49     //temporary solution until Hb specific style is ready
    61     setStyle(new QWindowsStyle);
    50     setStyle(new QWindowsStyle);
    62 #endif // QT_DEBUG
    51 #endif // QT_DEBUG
    63 
       
    64     // ignore command line arguments on Symbian
       
    65 #ifdef Q_OS_SYMBIAN
       
    66     Options::start = true;
       
    67     Options::persistent = true;
       
    68 #else
       
    69     QStringList args = arguments();
       
    70     args.removeFirst(); // ignore argv0
       
    71     const bool wasEmpty = args.isEmpty();
       
    72     const bool restart = args.removeAll(QLatin1String("-restart"));
       
    73     Options::start = args.removeAll(QLatin1String("-start")) || restart;
       
    74     Options::stop = args.removeAll(QLatin1String("-stop")) || restart;
       
    75     Options::persistent = args.removeAll(QLatin1String("-persistent"));
       
    76     Options::help = args.removeAll(QLatin1String("-help"))
       
    77                     || args.removeAll(QLatin1String("-h")) || !args.isEmpty() || wasEmpty;
       
    78     if (!args.isEmpty()) {
       
    79         Options::error = tr("Unknown option(s): '%1'").arg(args.join(QLatin1String(" ")));
       
    80     }
       
    81 #endif // Q_OS_SYMBIAN
       
    82 }
    52 }
    83 
    53 
    84 HbThemeServerApplication::~HbThemeServerApplication()
    54 HbThemeServerApplication::~HbThemeServerApplication()
    85 {
    55 {
    86     delete server;
    56     delete server;
    87 }
    57 }
    88 
    58 
    89 bool HbThemeServerApplication::initialize()
    59 bool HbThemeServerApplication::initialize()
    90 {
    60 {
    91 #if defined (Q_OS_SYMBIAN)
       
    92     CEikonEnv * env = CEikonEnv::Static();
    61     CEikonEnv * env = CEikonEnv::Static();
    93     if ( env ) {
    62     if ( env ) {
    94         _LIT(KHbThemeServer, "HbThemeServer");
    63         _LIT(KHbThemeServer, "HbThemeServer");
    95         CApaWindowGroupName *wgName = CApaWindowGroupName::NewLC(env->WsSession());
    64         CApaWindowGroupName *wgName = CApaWindowGroupName::NewLC(env->WsSession());
    96         env->RootWin().SetOrdinalPosition(0, ECoeWinPriorityNeverAtFront); // avoid coming to foreground
    65         env->RootWin().SetOrdinalPosition(0, ECoeWinPriorityNeverAtFront); // avoid coming to foreground
   104         CleanupStack::PopAndDestroy();
    73         CleanupStack::PopAndDestroy();
   105         RThread::RenameMe(KHbThemeServer);
    74         RThread::RenameMe(KHbThemeServer);
   106         User::SetProcessCritical(User::ESystemCritical);
    75         User::SetProcessCritical(User::ESystemCritical);
   107         User::SetCritical(User::ESystemCritical);
    76         User::SetCritical(User::ESystemCritical);
   108     }
    77     }
   109 #endif
       
   110 
    78 
   111     // load resource libraries in order to make binary resources accessible
    79     // load resource libraries in order to make binary resources accessible
   112     bool result = loadLibrary(RESOURCE_LIB_NAME);
    80     bool result = loadLibrary(RESOURCE_LIB_NAME);
   113 #ifdef HB_DEVELOPER
    81 #ifdef HB_DEVELOPER
   114     loadLibrary(TEST_RESOURCE_LIB_NAME);
    82     loadLibrary(TEST_RESOURCE_LIB_NAME);
   121     if (!server) {
    89     if (!server) {
   122         server = new HbThemeServer;
    90         server = new HbThemeServer;
   123     }
    91     }
   124 
    92 
   125     if (server->startServer()) {
    93     if (server->startServer()) {
   126 #if !defined(Q_OS_SYMBIAN) && defined(QT_DEBUG)
       
   127         server->showMinimized();
       
   128 #endif
       
   129         return QtSingleApplication::exec();
    94         return QtSingleApplication::exec();
   130     }
    95     }
   131 
    96 
   132     return EXIT_FAILURE;
    97     return EXIT_FAILURE;
   133 }
    98 }
   134 
    99 
   135 void HbThemeServerApplication::stop()
   100 bool HbThemeServerApplication::loadLibrary(const QString &name)
   136 {
       
   137 #ifndef Q_OS_SYMBIAN
       
   138     sendMessage(STOP_MESSAGE);
       
   139 #endif // Q_OS_SYMBIAN
       
   140 }
       
   141 
       
   142 static bool hb_loadLibraryHelper(const QString &name)
       
   143 {
   101 {
   144     QLibrary library(name);
   102     QLibrary library(name);
   145     // rely on dynamic loader (LD_LIBRARY_PATH)
   103     // rely on dynamic loader (LD_LIBRARY_PATH)
   146     bool result = library.load();
   104     bool result = library.load();
   147     if (!result) {
   105     if (!result) {
   153             QString path = QLatin1String(HB_BUILD_DIR) + QDir::separator() + QLatin1String("lib");
   111             QString path = QLatin1String(HB_BUILD_DIR) + QDir::separator() + QLatin1String("lib");
   154             library.setFileName(QDir(path).filePath(name));
   112             library.setFileName(QDir(path).filePath(name));
   155             result = library.load();
   113             result = library.load();
   156         }
   114         }
   157     }
   115     }
   158 #ifdef THEME_SERVER_TRACES
       
   159     if (!result) {
   116     if (!result) {
   160         qDebug() << "hb_loadLibraryHelper():" << library.errorString();
   117         THEME_GENERIC_DEBUG() << Q_FUNC_INFO << "Error: " << library.errorString();
   161     }
   118     }
   162 #endif
       
   163     return result;
   119     return result;
   164 }
       
   165 
       
   166 bool HbThemeServerApplication::loadLibrary(const QString &name)
       
   167 {
       
   168     // To load resources embedded in hb library
       
   169     bool result = hb_loadLibraryHelper(name);
       
   170     if (!result) {
       
   171         // Library may not be loaded, if it was built in debug mode and the name in debug mode is
       
   172         // different, change the name to debug version in that scenario
       
   173         QString alternateName = name;
       
   174 #ifdef Q_OS_WIN32
       
   175         alternateName += WIN32_DEBUG_SUFFIX;
       
   176 #elif defined(Q_OS_MAC)
       
   177         alternateName += MAC_DEBUG_SUFFIX;
       
   178 #endif
       
   179         // On symbian library name in debug mode is same as that in release mode,
       
   180         // so no need to do anything for that
       
   181         if (alternateName != name) {
       
   182             result = hb_loadLibraryHelper(alternateName);
       
   183         }
       
   184     }
       
   185 #ifdef THEME_SERVER_TRACES
       
   186     if (result) {
       
   187         qDebug() << "HbThemeServerApplication::loadLibrary(): loaded library " << name;
       
   188     }
       
   189     else {
       
   190         qDebug() << "HbThemeServerApplication::loadLibrary(): could not load library " << name;
       
   191     }
       
   192 #endif // THEME_SERVER_TRACES
       
   193 
       
   194     return result;
       
   195 }
       
   196 
       
   197 void HbThemeServerApplication::receiveMessage(const QString &message)
       
   198 {
       
   199     if (!server) {
       
   200         return;
       
   201     }
       
   202 
       
   203     if (message == STOP_MESSAGE) {
       
   204         server->stopServer();
       
   205         quit();
       
   206     }
       
   207 }
   120 }
   208 
   121 
   209 bool HbThemeServerLocker::lock()
   122 bool HbThemeServerLocker::lock()
   210 {
   123 {
   211 #ifdef Q_OS_SYMBIAN
       
   212     Lock::State lockState;
   124     Lock::State lockState;
   213     
   125     
   214     Q_FOREVER {
   126     Q_FOREVER {
   215         lockState = mLock.acquire();
   127         lockState = mLock.acquire();
   216         if (lockState == Lock::Reserved) {
   128         if (lockState == Lock::Reserved) {
   217             // Process may be starting, wait for server object to be created
   129             // Process may be starting, wait for server object to be created
   218             if (serverExists()) {
   130             if (serverExists()) {
   219 #ifdef THEME_SERVER_TRACES
   131                 THEME_GENERIC_DEBUG() << Q_FUNC_INFO << "server already exists.";
   220                 qDebug() << "HbThemeServerLocker::lock: serverExists";
       
   221 #endif
       
   222                 break;
   132                 break;
   223             } else {
   133             } else {
   224                 const TInt KTimeout = 100000; // 100 ms
   134                 const TInt KTimeout = 100000; // 100 ms
   225                 User::After(KTimeout);
   135                 User::After(KTimeout);
   226             }
   136             }
   229         }
   139         }
   230     }
   140     }
   231     
   141     
   232     if (lockState != Lock::Acquired) {
   142     if (lockState != Lock::Acquired) {
   233         // With KErrAlreadyExists client should try to connect, otherwise bail out.
   143         // With KErrAlreadyExists client should try to connect, otherwise bail out.
   234 #ifdef THEME_SERVER_TRACES
   144         THEME_GENERIC_DEBUG() << Q_FUNC_INFO << "Lock not acquired.";
   235         qDebug() << "HbThemeServer::main: Lock not acquired!!!";
       
   236 #endif
       
   237         RProcess::Rendezvous(lockState == Lock::Reserved ? KErrAlreadyExists : KErrGeneral);
   145         RProcess::Rendezvous(lockState == Lock::Reserved ? KErrAlreadyExists : KErrGeneral);
   238     }
   146     }
   239 
   147 
   240     return (lockState == Lock::Acquired);
   148     return (lockState == Lock::Acquired);
   241 #else
       
   242     return true;
       
   243 #endif
       
   244 }
   149 }
   245 void HbThemeServerApplication::setPriority()
   150 void HbThemeServerApplication::setPriority()
   246 {
   151 {
   247 #ifdef Q_OS_SYMBIAN
       
   248     RProcess().SetPriority(EPriorityHigh);
   152     RProcess().SetPriority(EPriorityHigh);
   249 #endif
       
   250 }
   153 }
   251 
   154 
   252 #ifdef Q_OS_SYMBIAN
       
   253 _LIT(KLockFileName, "lockFile");
   155 _LIT(KLockFileName, "lockFile");
   254 
   156 
   255 Lock::Lock()
   157 Lock::Lock()
   256 {
   158 {
   257     // Using a file for interprocess lock
   159     // Using a file for interprocess lock
   284 {
   186 {
   285     TFindServer findHbServer(KThemeServerName);
   187     TFindServer findHbServer(KThemeServerName);
   286     TFullName name;
   188     TFullName name;
   287     return findHbServer.Next(name) == KErrNone;
   189     return findHbServer.Next(name) == KErrNone;
   288 }
   190 }
   289 
       
   290 #endif