qtmobility/src/contacts/qcontactmanager_p.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
   139             foreach (QContactManagerEngineFactory* f, factories) {
   139             foreach (QContactManagerEngineFactory* f, factories) {
   140                 QList<int> versions = f->supportedImplementationVersions();
   140                 QList<int> versions = f->supportedImplementationVersions();
   141                 if (implementationVersion == -1 ||//no given implementation version required
   141                 if (implementationVersion == -1 ||//no given implementation version required
   142                         versions.isEmpty() || //the manager engine factory does not report any version
   142                         versions.isEmpty() || //the manager engine factory does not report any version
   143                         versions.contains(implementationVersion)) {
   143                         versions.contains(implementationVersion)) {
   144                     m_engine = f->engine(parameters, m_error);
   144                     m_engine = f->engine(parameters, &m_error);
   145                     found = true;
   145                     found = true;
   146                     break;
   146                     break;
   147                 }
   147                 }
   148             }
   148             }
   149             
   149             
   165         }
   165         }
   166 
   166 
   167         if (!m_engine) {
   167         if (!m_engine) {
   168             if (m_error == QContactManager::NoError)
   168             if (m_error == QContactManager::NoError)
   169                 m_error = QContactManager::DoesNotExistError;
   169                 m_error = QContactManager::DoesNotExistError;
   170             m_engine = new QContactInvalidEngine(); // XXX share
   170             m_engine = new QContactInvalidEngine();
   171         }
   171         }
   172     }
   172     }
   173 }
   173 }
   174 
   174 
   175 
   175 
   176 void QContactManagerData::loadStaticFactories()
   176 void QContactManagerData::loadStaticFactories()
   177 {
   177 {
   178     if (!m_discoveredStatic) {
   178     if (!m_discoveredStatic) {
       
   179 #if !defined QT_NO_DEBUG
       
   180         const bool showDebug = qgetenv("QT_DEBUG_PLUGINS").toInt() > 0;
       
   181 #endif
       
   182 
   179         m_discoveredStatic = true;
   183         m_discoveredStatic = true;
   180 
   184 
   181         /* Clean stuff up at the end */
   185         /* Clean stuff up at the end */
   182         qAddPostRoutine(qContactsCleanEngines);
   186         qAddPostRoutine(qContactsCleanEngines);
   183 
   187 
   186         for (int i=0; i < staticPlugins.count(); i++ ){
   190         for (int i=0; i < staticPlugins.count(); i++ ){
   187             QContactManagerEngineFactory *f = qobject_cast<QContactManagerEngineFactory*>(staticPlugins.at(i));
   191             QContactManagerEngineFactory *f = qobject_cast<QContactManagerEngineFactory*>(staticPlugins.at(i));
   188             QContactActionFactory *g = qobject_cast<QContactActionFactory*>(staticPlugins.at(i));
   192             QContactActionFactory *g = qobject_cast<QContactActionFactory*>(staticPlugins.at(i));
   189             if (f) {
   193             if (f) {
   190                 QString name = f->managerName();
   194                 QString name = f->managerName();
   191                 qDebug() << "Static: found an engine plugin" << f << "with name" << name;
   195 #if !defined QT_NO_DEBUG
       
   196                 if (showDebug)
       
   197                     qDebug() << "Static: found an engine plugin" << f << "with name" << name;
       
   198 #endif
   192                 if (name != QLatin1String("memory") && name != QLatin1String("invalid") && !name.isEmpty()) {
   199                 if (name != QLatin1String("memory") && name != QLatin1String("invalid") && !name.isEmpty()) {
   193                     // we also need to ensure that we haven't already loaded this factory.
   200                     // we also need to ensure that we haven't already loaded this factory.
   194                     if (m_engines.keys().contains(name)) {
   201                     if (m_engines.keys().contains(name)) {
   195                         qWarning() << "Static contacts plugin" << name << "has the same name as a currently loaded plugin; ignored";
   202                         qWarning() << "Static contacts plugin" << name << "has the same name as a currently loaded plugin; ignored";
   196                     } else {
   203                     } else {
   201                 }
   208                 }
   202             }
   209             }
   203 
   210 
   204             if (g) {
   211             if (g) {
   205                 QString name = g->name();
   212                 QString name = g->name();
   206                 qDebug() << "Static: found an action factory" << g << "with name" << name;
   213 #if !defined QT_NO_DEBUG
   207 
   214                 if (showDebug)
       
   215                     qDebug() << "Static: found an action factory" << g << "with name" << name;
       
   216 #endif
   208                 if (m_actionfactories.contains(g)) {
   217                 if (m_actionfactories.contains(g)) {
   209                     qWarning() << "Static contacts plugin" << name << "has the same name as currently loaded plugin; ignored";
   218                     qWarning() << "Static contacts plugin" << name << "has the same name as currently loaded plugin; ignored";
   210                 } else {
   219                 } else {
   211                     m_actionfactories.append(g);
   220                     m_actionfactories.append(g);
   212 
   221 
   223             }
   232             }
   224         }
   233         }
   225     }
   234     }
   226 }
   235 }
   227 
   236 
       
   237 class DirChecker
       
   238 {
       
   239 public:
       
   240     DirChecker();
       
   241     ~DirChecker();
       
   242     bool checkDir(const QDir& dir);
       
   243 
       
   244 private:
       
   245 #if defined(Q_OS_SYMBIAN)
       
   246     RFs rfs;
       
   247 #endif
       
   248 };
       
   249 
       
   250 #if defined(Q_OS_SYMBIAN)
       
   251 DirChecker::DirChecker()
       
   252 {
       
   253     qt_symbian_throwIfError(rfs.Connect());
       
   254 }
       
   255 
       
   256 bool DirChecker::checkDir(const QDir& dir)
       
   257 {
       
   258     bool pathFound = false;
       
   259     // In Symbian, going cdUp() in a c:/private/<uid3>/ will result in *platsec* error at fileserver (requires AllFiles capability)
       
   260     // Also, trying to cd() to a nonexistent directory causes *platsec* error. This does not cause functional harm, but should
       
   261     // nevertheless be changed to use native Symbian methods to avoid unnecessary platsec warnings (as per qpluginloader.cpp).
       
   262     // Use native Symbian code to check for directory existence, because checking
       
   263     // for files from under non-existent protected dir like E:/private/<uid> using
       
   264     // QDir::exists causes platform security violations on most apps.
       
   265     QString nativePath = QDir::toNativeSeparators(dir.absolutePath());
       
   266     TPtrC ptr = TPtrC16(static_cast<const TUint16*>(nativePath.utf16()), nativePath.length());
       
   267     TUint attributes;
       
   268     TInt err = rfs.Att(ptr, attributes);
       
   269     if (err == KErrNone) {
       
   270         // yes, the directory exists.
       
   271         pathFound = true;
       
   272     }
       
   273     return pathFound;
       
   274 }
       
   275 
       
   276 DirChecker::~DirChecker()
       
   277 {
       
   278     rfs.Close();
       
   279 }
       
   280 #else
       
   281 DirChecker::DirChecker()
       
   282 {
       
   283 }
       
   284 
       
   285 DirChecker::~DirChecker()
       
   286 {
       
   287 }
       
   288 
       
   289 bool DirChecker::checkDir(const QDir &dir)
       
   290 {
       
   291     return dir.exists();
       
   292 }
       
   293 #endif
       
   294 
   228 /* Plugin loader */
   295 /* Plugin loader */
   229 void QContactManagerData::loadFactories()
   296 void QContactManagerData::loadFactories()
   230 {
   297 {
       
   298 #if !defined QT_NO_DEBUG
       
   299     const bool showDebug = qgetenv("QT_DEBUG_PLUGINS").toInt() > 0;
       
   300 #endif
       
   301 
   231     // Always do this..
   302     // Always do this..
   232     loadStaticFactories();
   303     loadStaticFactories();
   233 
   304 
   234     if (!m_discovered || QApplication::libraryPaths() != m_pluginPaths) {
   305     if (!m_discovered || QApplication::libraryPaths() != m_pluginPaths) {
   235         m_discovered = true;
   306         m_discovered = true;
   240 
   311 
   241         QStringList paths;
   312         QStringList paths;
   242         QSet<QString> processed;
   313         QSet<QString> processed;
   243 
   314 
   244         paths << QApplication::applicationDirPath() << QApplication::libraryPaths();
   315         paths << QApplication::applicationDirPath() << QApplication::libraryPaths();
   245         qDebug() << "Plugin paths:" << paths;
   316 #if !defined QT_NO_DEBUG
       
   317         if (showDebug)
       
   318             qDebug() << "Plugin paths:" << paths;
       
   319 #endif
       
   320 
       
   321         DirChecker dirChecker;
   246 
   322 
   247         /* Enumerate our plugin paths */
   323         /* Enumerate our plugin paths */
   248         for (int i=0; i < paths.count(); i++) {
   324         for (int i=0; i < paths.count(); i++) {
   249             if (processed.contains(paths.at(i)))
   325             if (processed.contains(paths.at(i)))
   250                 continue;
   326                 continue;
   251             processed.insert(paths.at(i));
   327             processed.insert(paths.at(i));
   252             QDir pluginsDir(paths.at(i));
   328             QDir pluginsDir(paths.at(i));
   253             if (!pluginsDir.exists())
   329             if (!dirChecker.checkDir(pluginsDir))
   254                 continue;
   330                 continue;
   255 
   331 
   256 #if defined(Q_OS_WIN)
   332 #if defined(Q_OS_WIN)
   257             if (pluginsDir.dirName().toLower() == QLatin1String("debug") || pluginsDir.dirName().toLower() == QLatin1String("release"))
   333             if (pluginsDir.dirName().toLower() == QLatin1String("debug") || pluginsDir.dirName().toLower() == QLatin1String("release"))
   258                 pluginsDir.cdUp();
   334                 pluginsDir.cdUp();
   262                 pluginsDir.cdUp();
   338                 pluginsDir.cdUp();
   263                 pluginsDir.cdUp();
   339                 pluginsDir.cdUp();
   264             }
   340             }
   265 #endif
   341 #endif
   266 
   342 
   267 #if defined(Q_OS_SYMBIAN)
   343             QString subdir(QLatin1String("plugins/contacts"));
   268             // In Symbian, going cdUp() in a c:/private/<uid3>/ will result in *platsec* error at fileserver (requires AllFiles capability)
   344             if (pluginsDir.path().endsWith(QLatin1String("/plugins"))
   269             // Also, trying to cd() to a nonexistent directory causes *platsec* error. This does not cause functional harm, but should
   345                 || pluginsDir.path().endsWith(QLatin1String("/plugins/")))
   270             // nevertheless be changed to use native Symbian methods to avoid unnecessary platsec warnings (as per qpluginloader.cpp).
   346                 subdir = QLatin1String("contacts");
   271             RFs rfs;
   347 
   272             qt_symbian_throwIfError(rfs.Connect());
   348             if (dirChecker.checkDir(QDir(pluginsDir.path() + QLatin1Char('/') + subdir))) {
   273             bool pluginPathFound = false;
   349                 pluginsDir.cd(subdir);
   274             QStringList directories;
       
   275             directories << QString("plugins/contacts") << QString("contacts") << QString("../plugins/contacts");
       
   276             foreach (const QString& dirName, directories) {
       
   277                 QString testDirPath = pluginsDir.path() + "/" + dirName;
       
   278                 testDirPath = QDir::cleanPath(testDirPath);
       
   279                 // Use native Symbian code to check for directory existence, because checking
       
   280                 // for files from under non-existent protected dir like E:/private/<uid> using
       
   281                 // QDir::exists causes platform security violations on most apps.
       
   282                 QString nativePath = QDir::toNativeSeparators(testDirPath);
       
   283                 TPtrC ptr = TPtrC16(static_cast<const TUint16*>(nativePath.utf16()), nativePath.length());
       
   284                 TUint attributes;
       
   285                 TInt err = rfs.Att(ptr, attributes);
       
   286                 if (err == KErrNone) {
       
   287                     // yes, the directory exists.
       
   288                     pluginsDir.cd(testDirPath);
       
   289                     pluginPathFound = true;
       
   290                     break;
       
   291                 }
       
   292             }
       
   293             rfs.Close();
       
   294             if (pluginPathFound) {
       
   295 #else
       
   296             if (pluginsDir.cd(QLatin1String("plugins/contacts")) || pluginsDir.cd(QLatin1String("contacts")) || (pluginsDir.cdUp() && pluginsDir.cd(QLatin1String("plugins/contacts")))) {
       
   297 #endif
       
   298                 const QStringList& files = pluginsDir.entryList(QDir::Files);
   350                 const QStringList& files = pluginsDir.entryList(QDir::Files);
   299                 qDebug() << "Looking for plugins in" << pluginsDir.path() << files;
   351 #if !defined QT_NO_DEBUG
       
   352                 if (showDebug)
       
   353                     qDebug() << "Looking for contacts plugins in" << pluginsDir.path() << files;
       
   354 #endif
   300                 for (int j=0; j < files.count(); j++) {
   355                 for (int j=0; j < files.count(); j++) {
   301                     plugins << pluginsDir.absoluteFilePath(files.at(j));
   356                     plugins <<  pluginsDir.absoluteFilePath(files.at(j));
   302                 }
   357                 }
   303             }
   358             }
   304         }
   359         }
   305 
   360 
   306         /* Now discover the dynamic plugins */
   361         /* Now discover the dynamic plugins */
   309             QContactManagerEngineFactory *f = qobject_cast<QContactManagerEngineFactory*>(qpl.instance());
   364             QContactManagerEngineFactory *f = qobject_cast<QContactManagerEngineFactory*>(qpl.instance());
   310             QContactActionFactory *g = qobject_cast<QContactActionFactory*>(qpl.instance());
   365             QContactActionFactory *g = qobject_cast<QContactActionFactory*>(qpl.instance());
   311 
   366 
   312             if (f) {
   367             if (f) {
   313                 QString name = f->managerName();
   368                 QString name = f->managerName();
   314                 qDebug() << "Dynamic: found an engine plugin" << f << "with name" << name;
   369 #if !defined QT_NO_DEBUG
   315 
   370                 if (showDebug)
       
   371                     qDebug() << "Dynamic: found a contact engine plugin" << f << "with name" << name;
       
   372 #endif
   316                 if (name != QLatin1String("memory") && name != QLatin1String("invalid") && !name.isEmpty()) {
   373                 if (name != QLatin1String("memory") && name != QLatin1String("invalid") && !name.isEmpty()) {
   317                     // we also need to ensure that we haven't already loaded this factory.
   374                     // we also need to ensure that we haven't already loaded this factory.
   318                     if (m_engines.keys().contains(name)) {
   375                     if (m_engines.keys().contains(name)) {
   319                         qWarning() << "Contacts plugin" << plugins.at(i) << "has the same name as currently loaded plugin" << name << "; ignored";
   376                         qWarning() << "Contacts plugin" << plugins.at(i) << "has the same name as currently loaded plugin" << name << "; ignored";
   320                     } else {
   377                     } else {
   325                 }
   382                 }
   326             }
   383             }
   327 
   384 
   328             if (g) {
   385             if (g) {
   329                 QString name = g->name();
   386                 QString name = g->name();
   330                 qDebug() << "Dynamic: found an action factory" << g << "with name" << name;
   387 #if !defined QT_NO_DEBUG
   331 
   388                 if (showDebug)
       
   389                     qDebug() << "Dynamic: found a contact action factory" << g << "with name" << name;
       
   390 #endif
   332                 // we also need to ensure that we haven't already loaded this factory.
   391                 // we also need to ensure that we haven't already loaded this factory.
   333                 if (m_actionfactories.contains(g)) {
   392                 if (m_actionfactories.contains(g)) {
   334                     qWarning() << "Contacts plugin" << plugins.at(i) << "has the same name as currently loaded plugin" << name << "; ignored";
   393                     qWarning() << "Contacts plugin" << plugins.at(i) << "has the same name as currently loaded plugin" << name << "; ignored";
   335                 } else {
   394                 } else {
   336                     m_actionfactories.append(g);
   395                     m_actionfactories.append(g);
   346                     }
   405                     }
   347                 }
   406                 }
   348             }
   407             }
   349 
   408 
   350             /* Debugging */
   409             /* Debugging */
   351             if (!f && !g) {
   410 #if !defined QT_NO_DEBUG
       
   411             if (showDebug && !f && !g) {
   352                 qDebug() << "Unknown plugin:" << qpl.errorString();
   412                 qDebug() << "Unknown plugin:" << qpl.errorString();
   353                 if (qpl.instance()) {
   413                 if (qpl.instance()) {
   354                     qDebug() << "[qobject:" << qpl.instance() << "]";
   414                     qDebug() << "[qobject:" << qpl.instance() << "]";
   355                 }
   415                 }
   356             }
   416             }
       
   417 #endif
   357         }
   418         }
   358         
   419         
   359         QStringList engineNames;
   420         QStringList engineNames;
   360         foreach (QContactManagerEngineFactory* f, m_engines.values()) {
   421         foreach (QContactManagerEngineFactory* f, m_engines.values()) {
   361             QStringList versions;
   422             QStringList versions;
   362             foreach (int v, f->supportedImplementationVersions()) {
   423             foreach (int v, f->supportedImplementationVersions()) {
   363                 versions << QString::fromAscii("%1").arg(v);
   424                 versions << QString::fromAscii("%1").arg(v);
   364             }
   425             }
   365             engineNames << QString::fromAscii("%1[%2]").arg(f->managerName()).arg(versions.join(QString::fromAscii(",")));
   426             engineNames << QString::fromAscii("%1[%2]").arg(f->managerName()).arg(versions.join(QString::fromAscii(",")));
   366         }
   427         }
   367         qDebug() << "Found engines:" << engineNames;
   428 #if !defined QT_NO_DEBUG
   368         qDebug() << "Found actions:" << m_actionmap.keys();
   429         if (showDebug) {
       
   430             qDebug() << "Found engines:" << engineNames;
       
   431             qDebug() << "Found actions:" << m_actionmap.keys();
       
   432         }
       
   433 #endif
   369     }
   434     }
   370 }
   435 }
   371 
   436 
   372 QList<QContactActionDescriptor> QContactManagerData::actionDescriptors(const QString& actionName, const QString& vendorName, int implementationVersion)
   437 QList<QContactActionDescriptor> QContactManagerData::actionDescriptors(const QString& actionName, const QString& vendorName, int implementationVersion)
   373 {
   438 {