qtmobility/src/serviceframework/databasemanager.cpp
changeset 15 1f895d8a5b2b
parent 11 06b8e2af4411
equal deleted inserted replaced
14:6fbed849b4f4 15:1f895d8a5b2b
    76         return;
    76         return;
    77     }
    77     }
    78     if (existing == dbPath) {
    78     if (existing == dbPath) {
    79         ServiceDatabase *db = 0;
    79         ServiceDatabase *db = 0;
    80         DatabaseManager::DbScope scope;
    80         DatabaseManager::DbScope scope;
    81         if (dbPath == m_manager->m_userDb->databasePath()) {
    81         if (m_manager->m_userDb && dbPath == m_manager->m_userDb->databasePath()) {
    82             db = m_manager->m_userDb;
    82             db = m_manager->m_userDb;
    83             scope = DatabaseManager::UserOnlyScope;
    83             scope = DatabaseManager::UserOnlyScope;
    84         } else if (dbPath == m_manager->m_systemDb->databasePath()) {
    84         } else if (dbPath == m_manager->m_systemDb->databasePath()) {
    85             db = m_manager->m_systemDb;
    85             db = m_manager->m_systemDb;
    86             scope = DatabaseManager::SystemScope;
    86             scope = DatabaseManager::SystemScope;
   144     }
   144     }
   145 }
   145 }
   146 
   146 
   147 void DatabaseFileWatcher::databaseChanged(const QString &path)
   147 void DatabaseFileWatcher::databaseChanged(const QString &path)
   148 {
   148 {
   149     if (path == m_manager->m_userDb->databasePath())
   149     if (m_manager->m_userDb && path == m_manager->m_userDb->databasePath())
   150         notifyChanges(m_manager->m_userDb, DatabaseManager::UserScope);
   150         notifyChanges(m_manager->m_userDb, DatabaseManager::UserScope);
   151     else if (path == m_manager->m_systemDb->databasePath())
   151     else if (path == m_manager->m_systemDb->databasePath())
   152         notifyChanges(m_manager->m_systemDb, DatabaseManager::SystemScope);
   152         notifyChanges(m_manager->m_systemDb, DatabaseManager::SystemScope);
   153 
   153 
   154     // if database was deleted, the path may have been dropped
   154     // if database was deleted, the path may have been dropped
   156         m_watcher->addPath(path);
   156         m_watcher->addPath(path);
   157 }
   157 }
   158 
   158 
   159 void DatabaseFileWatcher::notifyChanges(ServiceDatabase *database, DatabaseManager::DbScope scope)
   159 void DatabaseFileWatcher::notifyChanges(ServiceDatabase *database, DatabaseManager::DbScope scope)
   160 {
   160 {
       
   161     #ifdef Q_OS_SYMBIAN
       
   162         scope = DatabaseManager::SystemScope;
       
   163     #endif
       
   164 
   161     QString dbPath = database->databasePath();
   165     QString dbPath = database->databasePath();
   162     if (!QFile::exists(dbPath)) {
   166     if (!QFile::exists(dbPath)) {
   163         m_knownServices.remove(dbPath);
   167         m_knownServices.remove(dbPath);
   164         restartDirMonitoring(dbPath, QString());
   168         restartDirMonitoring(dbPath, QString());
   165         return;
   169         return;
   239 
   243 
   240 /*
   244 /*
   241    Constructor
   245    Constructor
   242 */
   246 */
   243 DatabaseManager::DatabaseManager()
   247 DatabaseManager::DatabaseManager()
   244     : m_userDb(new ServiceDatabase),
   248     : m_userDb(NULL),
   245       m_systemDb(new ServiceDatabase),
   249       m_systemDb(new ServiceDatabase),
   246       m_fileWatcher(0),
   250       m_fileWatcher(0),
   247       m_hasAccessedUserDb(false),
   251       m_hasAccessedUserDb(false),
   248       m_alreadyWarnedOpenError(false)
   252       m_alreadyWarnedOpenError(false)
   249 {
   253 {
   250     initDbPath(UserScope);
   254     #ifndef Q_OS_SYMBIAN
       
   255         m_userDb = new ServiceDatabase;
       
   256         initDbPath(UserScope);
       
   257     #endif
   251     initDbPath(SystemScope);
   258     initDbPath(SystemScope);
   252 }
   259 }
   253 
   260 
   254 /*
   261 /*
   255    Destructor
   262    Destructor
   274     or m_systemDb, but does not open any
   281     or m_systemDb, but does not open any
   275     database connections
   282     database connections
   276 */
   283 */
   277 void DatabaseManager::initDbPath(DbScope scope)
   284 void DatabaseManager::initDbPath(DbScope scope)
   278 {
   285 {
       
   286     #ifdef Q_OS_SYMBIAN
       
   287         scope = SystemScope;
       
   288     #endif
       
   289 
   279     QSettings::Scope settingsScope;
   290     QSettings::Scope settingsScope;
   280     QString dbIdentifier;
   291     QString dbIdentifier;
   281     ServiceDatabase *db;
   292     ServiceDatabase *db;
   282     if (scope == SystemScope) {
   293     if (scope == SystemScope) {
   283         settingsScope = QSettings::SystemScope;
   294         settingsScope = QSettings::SystemScope;
   287         settingsScope = QSettings::UserScope;
   298         settingsScope = QSettings::UserScope;
   288         dbIdentifier = "_user";
   299         dbIdentifier = "_user";
   289         db = m_userDb;
   300         db = m_userDb;
   290     }
   301     }
   291 
   302 
   292     QSettings settings(QSettings::IniFormat, settingsScope,
   303     #ifdef Q_OS_SYMBIAN
   293             QLatin1String("Nokia"), QLatin1String("QtServiceFramework"));
   304         QDir dir(QDir::toNativeSeparators("C:\\Data\\temp\\QtServiceFW"));
   294     QFileInfo fi(settings.fileName());
   305     #else
   295     QDir dir = fi.dir();
   306         QSettings settings(QSettings::IniFormat, settingsScope,
       
   307                 QLatin1String("Nokia"), QLatin1String("QtServiceFramework"));
       
   308         QFileInfo fi(settings.fileName());
       
   309         QDir dir = fi.dir();
       
   310     #endif
   296     QString qtVersion(qVersion());
   311     QString qtVersion(qVersion());
   297     qtVersion = qtVersion.left(qtVersion.size() -2); //strip off patch version
   312     qtVersion = qtVersion.left(qtVersion.size() -2); //strip off patch version
   298     QString dbName = QString("QtServiceFramework_") + qtVersion + dbIdentifier + QLatin1String(".db");
   313     QString dbName = QString("QtServiceFramework_") + qtVersion + dbIdentifier + QLatin1String(".db");
   299     db->setDatabasePath(dir.path() + QDir::separator() + dbName);
   314     db->setDatabasePath(dir.path() + QDir::separator() + dbName);
   300 }
   315 }
   306     Returns true if the operation succeeded and false otherwise.
   321     Returns true if the operation succeeded and false otherwise.
   307     The last error is set when this function is called.
   322     The last error is set when this function is called.
   308 */
   323 */
   309 bool DatabaseManager::registerService(ServiceMetaDataResults &service, DbScope scope)
   324 bool DatabaseManager::registerService(ServiceMetaDataResults &service, DbScope scope)
   310 {
   325 {
       
   326     #ifdef Q_OS_SYMBIAN
       
   327         scope = SystemScope;
       
   328     #endif
       
   329 
   311     if (scope == DatabaseManager::SystemScope) {
   330     if (scope == DatabaseManager::SystemScope) {
   312         if(!openDb(DatabaseManager::SystemScope)) {
   331         if(!openDb(DatabaseManager::SystemScope)) {
   313             return false;
   332             return false;
   314         }  else {
   333         }  else {
   315             if (!m_systemDb->registerService(service)) {
   334             if (!m_systemDb->registerService(service)) {
   342     Returns true if the operation succeeded, false otherwise.
   361     Returns true if the operation succeeded, false otherwise.
   343     The last error is set when this function is called.
   362     The last error is set when this function is called.
   344 */
   363 */
   345 bool DatabaseManager::unregisterService(const QString &serviceName, DbScope scope)
   364 bool DatabaseManager::unregisterService(const QString &serviceName, DbScope scope)
   346 {
   365 {
       
   366     #ifdef Q_OS_SYMBIAN
       
   367         scope = SystemScope;
       
   368     #endif
       
   369 
   347     if(scope == DatabaseManager::SystemScope) {
   370     if(scope == DatabaseManager::SystemScope) {
   348         if (!openDb(DatabaseManager::SystemScope))
   371         if (!openDb(DatabaseManager::SystemScope))
   349             return false;
   372             return false;
   350    else {
   373    else {
   351             if (!m_systemDb->unregisterService(serviceName)) {
   374             if (!m_systemDb->unregisterService(serviceName)) {
   370         }
   393         }
   371     }
   394     }
   372 }
   395 }
   373 
   396 
   374 /*
   397 /*
       
   398     Removes the initialization specific information of \serviceName from the database
       
   399     corresponding to a \scope.
       
   400 
       
   401     Returns true if teh operation succeeded, false otherwise.
       
   402     The last error is set when this function is called.
       
   403   */
       
   404 bool DatabaseManager::serviceInitialized(const QString &serviceName, DbScope scope)
       
   405 {
       
   406     ServiceDatabase *db = (scope == DatabaseManager::SystemScope) ? m_systemDb : m_userDb;
       
   407 
       
   408     if (!openDb(scope)) {
       
   409         return false;
       
   410     } else {
       
   411         if (!db->serviceInitialized(serviceName)) {
       
   412             m_lastError = db->lastError();
       
   413             return false;
       
   414         } else {
       
   415             m_lastError.setError(DBError::NoError);
       
   416             return true;
       
   417         }
       
   418     }
       
   419 }
       
   420 
       
   421 /*
   375     Retrieves a list of interface descriptors that fulfill the constraints specified
   422     Retrieves a list of interface descriptors that fulfill the constraints specified
   376     by \a filter at a given \a scope.
   423     by \a filter at a given \a scope.
   377 
   424 
   378     The last error is set when this function is called.
   425     The last error is set when this function is called.
   379 */
   426 */
   380 QList<QServiceInterfaceDescriptor>  DatabaseManager::getInterfaces(const QServiceFilter &filter, DbScope scope)
   427 QList<QServiceInterfaceDescriptor>  DatabaseManager::getInterfaces(const QServiceFilter &filter, DbScope scope)
   381 {
   428 {
       
   429     #ifdef Q_OS_SYMBIAN
       
   430         QService::Scope requestedScope;
       
   431         if (scope == UserScope) {
       
   432             requestedScope = QService::UserScope;
       
   433         } else {
       
   434             requestedScope = QService::SystemScope;
       
   435         }
       
   436         scope = SystemScope;
       
   437     #endif
       
   438 
   382     QList<QServiceInterfaceDescriptor> descriptors;
   439     QList<QServiceInterfaceDescriptor> descriptors;
   383 
   440 
   384     int userDescriptorCount = 0;
   441     int userDescriptorCount = 0;
   385     if (scope == UserScope) {
   442     if (scope == UserScope) {
   386         if (!openDb(UserScope))
   443         if (!openDb(UserScope))
   406             m_lastError = m_systemDb->lastError();
   463             m_lastError = m_systemDb->lastError();
   407             return descriptors;
   464             return descriptors;
   408         }
   465         }
   409 
   466 
   410         for (int i = userDescriptorCount; i < descriptors.count(); ++i) {
   467         for (int i = userDescriptorCount; i < descriptors.count(); ++i) {
   411             descriptors[i].d->scope = QService::SystemScope;
   468             #ifdef Q_OS_SYMBIAN
       
   469                 descriptors[i].d->scope = requestedScope;
       
   470             #else
       
   471                 descriptors[i].d->scope = QService::SystemScope;
       
   472             #endif
   412         }
   473         }
   413     } else {
   474     } else {
   414         if ( scope == SystemScope) {
   475         if ( scope == SystemScope) {
   415             //openDb() should already have handled lastError
   476             //openDb() should already have handled lastError
   416             descriptors.clear();
   477             descriptors.clear();
   429 
   490 
   430     The last error is set when this function is called.
   491     The last error is set when this function is called.
   431 */
   492 */
   432 QStringList DatabaseManager::getServiceNames(const QString &interfaceName, DatabaseManager::DbScope scope)
   493 QStringList DatabaseManager::getServiceNames(const QString &interfaceName, DatabaseManager::DbScope scope)
   433 {
   494 {
       
   495     #ifdef Q_OS_SYMBIAN
       
   496         scope = SystemScope;
       
   497     #endif
       
   498 
   434     QStringList serviceNames;
   499     QStringList serviceNames;
   435     if (scope == UserScope || scope == UserOnlyScope) {
   500     if (scope == UserScope || scope == UserOnlyScope) {
   436         if(!openDb(DatabaseManager::UserScope))
   501         if(!openDb(DatabaseManager::UserScope))
   437             return serviceNames;
   502             return serviceNames;
   438         serviceNames = m_userDb->getServiceNames(interfaceName);
   503         serviceNames = m_userDb->getServiceNames(interfaceName);
   478 
   543 
   479     The last error is set when this function is called.
   544     The last error is set when this function is called.
   480 */
   545 */
   481 QServiceInterfaceDescriptor DatabaseManager::interfaceDefault(const QString &interfaceName, DbScope scope)
   546 QServiceInterfaceDescriptor DatabaseManager::interfaceDefault(const QString &interfaceName, DbScope scope)
   482 {
   547 {
       
   548     #ifdef Q_OS_SYMBIAN
       
   549         QService::Scope requestedScope;
       
   550         if (scope == UserScope) {
       
   551             requestedScope = QService::UserScope;
       
   552         } else {
       
   553             requestedScope = QService::SystemScope;
       
   554         }
       
   555         scope = SystemScope;
       
   556     #endif
       
   557 
   483     QServiceInterfaceDescriptor descriptor;
   558     QServiceInterfaceDescriptor descriptor;
   484     if (scope == UserScope) {
   559     if (scope == UserScope) {
   485         if (!openDb(UserScope))
   560         if (!openDb(UserScope))
   486             return QServiceInterfaceDescriptor();
   561             return QServiceInterfaceDescriptor();
   487         QString interfaceID;
   562         QString interfaceID;
   542     //or because we're specifically only querying at system scope
   617     //or because we're specifically only querying at system scope
   543     if (!openDb(SystemScope)) {
   618     if (!openDb(SystemScope)) {
   544         if (scope == SystemScope) {
   619         if (scope == SystemScope) {
   545             m_lastError = m_systemDb->lastError();
   620             m_lastError = m_systemDb->lastError();
   546             return QServiceInterfaceDescriptor();
   621             return QServiceInterfaceDescriptor();
   547         } else if (scope == UserScope && m_userDb->lastError().code() == DBError::NotFound) {
   622         } else if (scope == UserScope && m_userDb && m_userDb->lastError().code() == DBError::NotFound) {
   548             m_lastError = m_userDb->lastError();
   623             m_lastError = m_userDb->lastError();
   549             return QServiceInterfaceDescriptor();
   624             return QServiceInterfaceDescriptor();
   550         }
   625         }
   551     } else {
   626     } else {
   552         descriptor = m_systemDb->interfaceDefault(interfaceName);
   627         descriptor = m_systemDb->interfaceDefault(interfaceName);
   553         if (m_systemDb->lastError().code() == DBError::NoError) {
   628         if (m_systemDb->lastError().code() == DBError::NoError) {
   554             descriptor.d->scope = QService::SystemScope;
   629             #ifdef Q_OS_SYMBIAN
       
   630                 descriptor.d->scope = requestedScope;
       
   631             #else
       
   632                 descriptor.d->scope = QService::SystemScope;
       
   633             #endif
   555             return descriptor;
   634             return descriptor;
   556         } else if (m_systemDb->lastError().code() == DBError::NotFound) {
   635         } else if (m_systemDb->lastError().code() == DBError::NotFound) {
   557             m_lastError = m_systemDb->lastError();
   636             m_lastError = m_systemDb->lastError();
   558             return QServiceInterfaceDescriptor();
   637             return QServiceInterfaceDescriptor();
   559         } else {
   638         } else {
   615     Returns true if the operation succeeded, false otherwise.
   694     Returns true if the operation succeeded, false otherwise.
   616     The last error is set when this function is called.
   695     The last error is set when this function is called.
   617 */
   696 */
   618 bool DatabaseManager::setInterfaceDefault(const QServiceInterfaceDescriptor &descriptor, DbScope scope)
   697 bool DatabaseManager::setInterfaceDefault(const QServiceInterfaceDescriptor &descriptor, DbScope scope)
   619 {
   698 {
   620     if (scope == UserScope) {
   699     #ifdef Q_OS_SYMBIAN
   621         if (!openDb(UserScope))
   700         Q_UNUSED(scope);
       
   701         if (!openDb(SystemScope)) {
   622             return false;
   702             return false;
   623         if (descriptor.scope() == QService::UserScope) { //if a user scope descriptor, just set it in the user db
   703         } else {
   624             if(m_userDb->setInterfaceDefault(descriptor)) {
   704             if (m_systemDb->setInterfaceDefault(descriptor)) {
   625                 m_lastError.setError(DBError::NoError);
   705                 m_lastError.setError(DBError::NoError);
   626                 return true;
   706                 return true;
   627             } else {
   707             } else {
   628                 m_lastError = m_userDb->lastError();
   708                 m_lastError = m_systemDb->lastError();
   629                 return false;
   709                 return false;
   630             }
   710             }
   631         } else { //otherwise we need to get the interfaceID from the system db and set this
   711         }
   632                  //as an external default interface ID in the user db
   712     #else
   633             if (!openDb(SystemScope))
   713         if (scope == UserScope) {
       
   714             if (!openDb(UserScope))
   634                 return false;
   715                 return false;
   635 
   716             if (descriptor.scope() == QService::UserScope) { //if a user scope descriptor, just set it in the user db
   636             QString interfaceDescriptorID = m_systemDb->getInterfaceID(descriptor);
   717                 if(m_userDb->setInterfaceDefault(descriptor)) {
   637             if (m_systemDb->lastError().code() == DBError::NoError) {
       
   638                 if(m_userDb->setInterfaceDefault(descriptor, interfaceDescriptorID)) {
       
   639                     m_lastError.setError(DBError::NoError);
   718                     m_lastError.setError(DBError::NoError);
   640                     return true;
   719                     return true;
   641                 } else {
   720                 } else {
   642                     m_lastError = m_userDb->lastError();
   721                     m_lastError = m_userDb->lastError();
   643                     return false;
   722                     return false;
   644                 }
   723                 }
   645             } else {
   724             } else { //otherwise we need to get the interfaceID from the system db and set this
   646                 m_lastError = m_systemDb->lastError();
   725                      //as an external default interface ID in the user db
   647                 return false;
   726                 if (!openDb(SystemScope))
   648             }
   727                     return false;
   649         }
   728 
   650     } else {  //scope == SystemScope
   729                 QString interfaceDescriptorID = m_systemDb->getInterfaceID(descriptor);
   651         if (descriptor.scope() == QService::UserScope) {
   730                 if (m_systemDb->lastError().code() == DBError::NoError) {
   652             QString errorText("Cannot set default service at system scope with a user scope "
   731                     if(m_userDb->setInterfaceDefault(descriptor, interfaceDescriptorID)) {
   653                                 "interface descriptor");
   732                         m_lastError.setError(DBError::NoError);
   654             m_lastError.setError(DBError::InvalidDescriptorScope, errorText);
   733                         return true;
   655             return false;
   734                     } else {
   656         } else {
   735                         m_lastError = m_userDb->lastError();
   657             if (!openDb(SystemScope)) {
   736                         return false;
   658                 return false;
   737                     }
   659             } else {
       
   660                 if (m_systemDb->setInterfaceDefault(descriptor)) {
       
   661                     m_lastError.setError(DBError::NoError);
       
   662                     return true;
       
   663                 } else {
   738                 } else {
   664                     m_lastError = m_systemDb->lastError();
   739                     m_lastError = m_systemDb->lastError();
   665                     return false;
   740                     return false;
   666                 }
   741                 }
   667             }
   742             }
   668         }
   743         } else {  //scope == SystemScope
   669     }
   744             if (descriptor.scope() == QService::UserScope) {
       
   745                 QString errorText("Cannot set default service at system scope with a user scope "
       
   746                                     "interface descriptor");
       
   747                 m_lastError.setError(DBError::InvalidDescriptorScope, errorText);
       
   748                 return false;
       
   749             } else {
       
   750                 if (!openDb(SystemScope)) {
       
   751                     return false;
       
   752                 } else {
       
   753                     if (m_systemDb->setInterfaceDefault(descriptor)) {
       
   754                         m_lastError.setError(DBError::NoError);
       
   755                         return true;
       
   756                     } else {
       
   757                         m_lastError = m_systemDb->lastError();
       
   758                         return false;
       
   759                     }
       
   760                 }
       
   761             }
       
   762         }
       
   763     #endif
   670 }
   764 }
   671 
   765 
   672 /*
   766 /*
   673     Opens a database connection with the database at a specific \a scope.
   767     Opens a database connection with the database at a specific \a scope.
   674 
   768 
   675     The last error is set when this function is called.
   769     The last error is set when this function is called.
   676 */
   770 */
   677 bool DatabaseManager::openDb(DbScope scope)
   771 bool DatabaseManager::openDb(DbScope scope)
   678 {
   772 {
       
   773     #ifdef Q_OS_SYMBIAN
       
   774         scope = SystemScope;
       
   775     #endif
       
   776 
   679     if (scope == SystemScope && m_systemDb->isOpen() && !QFile::exists(m_systemDb->databasePath())) {
   777     if (scope == SystemScope && m_systemDb->isOpen() && !QFile::exists(m_systemDb->databasePath())) {
   680         delete m_systemDb;
   778         delete m_systemDb;
   681         m_systemDb = new ServiceDatabase;
   779         m_systemDb = new ServiceDatabase;
   682         initDbPath(SystemScope);
   780         initDbPath(SystemScope);
   683         m_alreadyWarnedOpenError = false;
   781         m_alreadyWarnedOpenError = false;
   737     }
   835     }
   738 
   836 
   739     //if we are opening the system database while the user database is open,
   837     //if we are opening the system database while the user database is open,
   740     //cleanup and reset any old external defaults
   838     //cleanup and reset any old external defaults
   741     //from the user scope database
   839     //from the user scope database
   742     if (scope == SystemScope && m_userDb->isOpen()) {
   840     if (scope == SystemScope && m_userDb && m_userDb->isOpen()) {
   743         QList<QPair<QString,QString> > externalDefaultsInfo;
   841         QList<QPair<QString,QString> > externalDefaultsInfo;
   744         externalDefaultsInfo = m_userDb->externalDefaultsInfo();
   842         externalDefaultsInfo = m_userDb->externalDefaultsInfo();
   745         QServiceInterfaceDescriptor descriptor;
   843         QServiceInterfaceDescriptor descriptor;
   746         QPair<QString,QString> defaultInfo;
   844         QPair<QString,QString> defaultInfo;
   747 
   845 
   788     Sets whether change notifications for added and removed services are
   886     Sets whether change notifications for added and removed services are
   789     \a enabled or not at a given \a scope.
   887     \a enabled or not at a given \a scope.
   790 */
   888 */
   791 void DatabaseManager::setChangeNotificationsEnabled(DbScope scope, bool enabled)
   889 void DatabaseManager::setChangeNotificationsEnabled(DbScope scope, bool enabled)
   792 {
   890 {
       
   891     #ifdef Q_OS_SYMBIAN
       
   892         scope = SystemScope;
       
   893     #endif
       
   894 
   793     if (!m_fileWatcher) m_fileWatcher = new
   895     if (!m_fileWatcher) m_fileWatcher = new
   794     DatabaseFileWatcher(this); m_fileWatcher->setEnabled(scope == SystemScope ?
   896     DatabaseFileWatcher(this); m_fileWatcher->setEnabled(scope == SystemScope ?
   795             m_systemDb : m_userDb, enabled);
   897             m_systemDb : m_userDb, enabled);
   796 }
   898 }
   797 
   899