~ubuntu-branches/ubuntu/quantal/qgis/quantal

« back to all changes in this revision

Viewing changes to src/qgsproviderregistry.cpp

  • Committer: Bazaar Package Importer
  • Author(s): William Grant
  • Date: 2007-05-06 13:42:32 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070506134232-pyli6t388w5asd8x
Tags: 0.8.0-3ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
  - debian/rules, debian/qgis.install, debian/qgis.dirs debian/qgis.desktop:
    Add and install .desktop.
* debian/qgis.desktop: Remove Applications category; it's not real.
* Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
                    qgsproviderregistry.cpp  -  Singleton class for
3
 
                    registering data providers.
4
 
                             -------------------
5
 
    begin                : Sat Jan 10 2004
6
 
    copyright            : (C) 2004 by Gary E.Sherman
7
 
    email                : sherman at mrcc.com
8
 
 ***************************************************************************/
9
 
 
10
 
/***************************************************************************
11
 
 *                                                                         *
12
 
 *   This program is free software; you can redistribute it and/or modify  *
13
 
 *   it under the terms of the GNU General Public License as published by  *
14
 
 *   the Free Software Foundation; either version 2 of the License, or     *
15
 
 *   (at your option) any later version.                                   *
16
 
 *                                                                         *
17
 
 ***************************************************************************/
18
 
 /* $Id: qgsproviderregistry.cpp,v 1.8.10.1 2005/07/09 23:51:20 timlinux Exp $ */
19
 
 
20
 
#include <iostream>
21
 
#include <qmessagebox.h>
22
 
#include <qstring.h>
23
 
#include <qdir.h>
24
 
#include <qlibrary.h>
25
 
#include <qapplication.h>
26
 
#include "qgsprovidermetadata.h"
27
 
#include "qgsproviderregistry.h"
28
 
// typedefs for provider plugin functions of interest
29
 
typedef QString providerkey_t();
30
 
typedef QString description_t();
31
 
typedef bool isprovider_t();
32
 
 
33
 
QgsProviderRegistry *QgsProviderRegistry::_instance = 0;
34
 
QgsProviderRegistry *QgsProviderRegistry::instance(QString pluginPath)
35
 
{
36
 
  if (_instance == 0)
37
 
    {
38
 
      _instance = new QgsProviderRegistry(pluginPath);
39
 
    }
40
 
  return _instance;
41
 
}
42
 
 
43
 
QgsProviderRegistry::QgsProviderRegistry(QString pluginPath)
44
 
{
45
 
// At startup, examine the libs in the qgis/lib dir and store those that
46
 
// are a provider shared lib
47
 
// check all libs in the current plugin directory and get name and descriptions
48
 
//TODO figure out how to register and identify data source plugin for a specific
49
 
//TODO layer type
50
 
/* char **argv = qApp->argv();
51
 
QString appDir = argv[0];
52
 
int bin = appDir.findRev("/bin", -1, false);
53
 
QString baseDir = appDir.left(bin);
54
 
QString libDir = baseDir + "/lib"; */
55
 
  libDir = pluginPath;
56
 
#ifdef WIN32
57
 
  QDir pluginDir(libDir, "*.dll", QDir::Name | QDir::IgnoreCase, QDir::Files | QDir::NoSymLinks);
58
 
#else
59
 
  QDir pluginDir(libDir, "*.so*", QDir::Name | QDir::IgnoreCase, QDir::Files | QDir::NoSymLinks);
60
 
#endif
61
 
#ifdef QGISDEBUG
62
 
  std::cerr << "Checking " << libDir.local8Bit() << " for provider plugins" << std::endl;
63
 
#endif
64
 
  if (pluginDir.count() == 0)
65
 
    {
66
 
      QString msg = QObject::tr("No Data Provider Plugins", "No QGIS data provider plugins found in:");
67
 
      msg += "\n" + libDir + "\n\n";
68
 
      msg += QObject::tr("No vector layers can be loaded. Check your QGIS installation");
69
 
      QMessageBox::critical(0, QObject::tr("No Data Providers"), msg);
70
 
  } else
71
 
    {
72
 
 
73
 
      for (unsigned i = 0; i < pluginDir.count(); i++)
74
 
        {
75
 
          QLibrary *myLib = new QLibrary(libDir + "/" + pluginDir[i]);
76
 
          bool loaded = myLib->load();
77
 
          if (loaded)
78
 
            {
79
 
#ifdef QGISDEBUG                    
80
 
              std::cout << "Checking  " << myLib->library().local8Bit() << std::endl;
81
 
#endif
82
 
              // get the description and the key for the provider plugin
83
 
 
84
 
              isprovider_t *isProvider = (isprovider_t *) myLib->resolve("isProvider");
85
 
 
86
 
              if (isProvider)
87
 
                {
88
 
                  // check to see if this is a provider plugin
89
 
                  if (isProvider())
90
 
                    {
91
 
                      // looks like a provider. get the key and description
92
 
                      description_t *pDesc = (description_t *) myLib->resolve("description");
93
 
                      providerkey_t *pKey = (providerkey_t *) myLib->resolve("providerKey");
94
 
                      if (pDesc && pKey)
95
 
                        {
96
 
                                                        const char *foo = pKey().local8Bit();
97
 
                          // add this provider to the provider map
98
 
                          provider[pKey()] = new QgsProviderMetadata(pKey(), pDesc(), myLib->library());
99
 
#ifdef QGISDEBUG
100
 
                          std::cout << "Loaded " << pDesc().local8Bit() << std::endl;
101
 
#endif
102
 
                      } else
103
 
                        {
104
 
                          std::cout << myLib->
105
 
                            library().local8Bit() << " Unable to find one of the required provider functions:\n\tproviderKey() or description()" <<
106
 
                            std::endl;
107
 
                        }
108
 
                    }
109
 
                }
110
 
            }
111
 
          delete myLib;
112
 
        }
113
 
    }
114
 
}
115
 
QString QgsProviderRegistry::library(QString providerKey)
116
 
{
117
 
  QString retval;
118
 
  QgsProviderMetadata *md = provider[providerKey];
119
 
  if (md)
120
 
    {
121
 
      retval = md->library();
122
 
    }
123
 
  return retval;
124
 
}
125
 
 
126
 
QString QgsProviderRegistry::pluginList(bool asHTML)
127
 
{
128
 
  std::map < QString, QgsProviderMetadata * >::iterator it = provider.begin();
129
 
  QString list;
130
 
  if (provider.size() == 0)
131
 
    {
132
 
      list = QObject::tr("No data provider plugins are available. No vector layers can be loaded");
133
 
  } else
134
 
    {
135
 
      if (asHTML)
136
 
        {
137
 
          list += "<ol>";
138
 
        }
139
 
      while (it != provider.end())
140
 
        {
141
 
          QgsProviderMetadata *mp = (*it).second;
142
 
          if (asHTML)
143
 
            {
144
 
              list += "<li>" + mp->description() + "<br>";
145
 
          } else
146
 
            {
147
 
              list += mp->description() + "\n";
148
 
            }
149
 
          it++;
150
 
        }
151
 
      if (asHTML)
152
 
        {
153
 
          list += "</ol>";
154
 
        }
155
 
    }
156
 
  return list;
157
 
}
158
 
 
159
 
void QgsProviderRegistry::setLibDirectory(QString path)
160
 
{
161
 
  libDir = path;
162
 
}
163
 
 
164
 
QString QgsProviderRegistry::libDirectory()
165
 
{
166
 
  return libDir;
167
 
}