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

« back to all changes in this revision

Viewing changes to src/qgspluginmanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve Halasz
  • Date: 2004-12-21 09:46:27 UTC
  • Revision ID: james.westby@ubuntu.com-20041221094627-r9lb6mlz2o3yp8gn
Tags: upstream-0.6.0
ImportĀ upstreamĀ versionĀ 0.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          qgspluginmanager.cpp  -  description
 
3
                             -------------------
 
4
    begin                : Someday 2003
 
5
    copyright            : (C) 2003 by Gary E.Sherman
 
6
    email                : sherman at mrcc.com
 
7
***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
11
 *   This program is free software; you can redistribute it and/or modify  *
 
12
 *   it under the terms of the GNU General Public License as published by  *
 
13
 *   the Free Software Foundation; either version 2 of the License, or     *
 
14
 *   (at your option) any later version.                                   *
 
15
 *                                                                         *
 
16
 ***************************************************************************/
 
17
/* $Id: qgspluginmanager.cpp,v 1.17 2004/11/27 19:19:16 gsherman Exp $ */
 
18
#include <iostream>
 
19
#include <qapplication.h>
 
20
#include <qfiledialog.h>
 
21
#include <qlineedit.h>
 
22
#include <qlistview.h>
 
23
#include <qmessagebox.h>
 
24
#include <qlibrary.h>
 
25
#include <qsettings.h>
 
26
#include "../plugins/qgisplugin.h"
 
27
#include "qgspluginmanager.h"
 
28
#include "qgspluginitem.h"
 
29
#include "qgsproviderregistry.h"
 
30
#include "qgspluginregistry.h"
 
31
 
 
32
 
 
33
#define TESTLIB
 
34
#ifdef TESTLIB
 
35
#ifndef WIN32
 
36
#include <dlfcn.h>
 
37
#endif
 
38
#endif
 
39
QgsPluginManager::QgsPluginManager(QWidget * parent, const char *name):QgsPluginManagerBase(parent, name)
 
40
{
 
41
  // set the default lib dir to the qgis install directory/lib (this info is
 
42
  // available from the provider registry so we use it here)
 
43
  QgsProviderRegistry *pr = QgsProviderRegistry::instance();
 
44
  /*  char **argv = qApp->argv();
 
45
     QString appDir = argv[0];
 
46
     int bin = appDir.findRev("/bin", -1, false);
 
47
     QString baseDir = appDir.left(bin);
 
48
     QString libDir = baseDir + "/lib"; */
 
49
 
 
50
  txtPluginDir->setText(pr->libDirectory());
 
51
  getPluginDescriptions();
 
52
}
 
53
 
 
54
 
 
55
QgsPluginManager::~QgsPluginManager()
 
56
{
 
57
}
 
58
 
 
59
void QgsPluginManager::browseFiles()
 
60
{
 
61
  QString s = QFileDialog::getExistingDirectory(0, this, "get existing directory", tr("Choose a directory"), TRUE);
 
62
  txtPluginDir->setText(s);
 
63
  getPluginDescriptions();
 
64
}
 
65
 
 
66
void QgsPluginManager::getPluginDescriptions()
 
67
{
 
68
QString sharedLibExtension;
 
69
#ifdef WIN32
 
70
sharedLibExtension = "*.dll";
 
71
#else
 
72
sharedLibExtension = "*.so*";
 
73
#endif
 
74
 
 
75
// check all libs in the current plugin directory and get name and descriptions
 
76
  QDir pluginDir(txtPluginDir->text(), sharedLibExtension, QDir::Name | QDir::IgnoreCase, QDir::Files | QDir::NoSymLinks);
 
77
 
 
78
  if (pluginDir.count() == 0)
 
79
    {
 
80
      QMessageBox::information(this, tr("No Plugins"), tr("No QGIS plugins found in ") + txtPluginDir->text());
 
81
  } else
 
82
    {
 
83
      std::cout << "PLUGIN MANAGER:" << std::endl;
 
84
      for (unsigned i = 0; i < pluginDir.count(); i++)
 
85
        {
 
86
#ifdef TESTLIB
 
87
#ifndef WIN32
 
88
          // test code to help debug loading problems
 
89
          QString lib = QString("%1/%2").arg(txtPluginDir->text()).arg(pluginDir[i]);
 
90
//          void *handle = dlopen((const char *) lib, RTLD_LAZY);
 
91
          void *handle = dlopen((const char *) lib, RTLD_LAZY | RTLD_GLOBAL);
 
92
          if (!handle)
 
93
            {
 
94
              std::cout << "Error in dlopen: " << dlerror() << std::endl;
 
95
 
 
96
          } else
 
97
            {
 
98
              std::cout << "dlopen suceeded" << std::endl;
 
99
              dlclose(handle);
 
100
            }
 
101
#endif //#ifndef WIN32
 
102
#endif //#ifdef TESTLIB
 
103
 
 
104
 
 
105
          std::cout << "Examining " << txtPluginDir->text() << "/" << pluginDir[i] << std::endl;
 
106
          QLibrary *myLib = new QLibrary(txtPluginDir->text() + "/" + pluginDir[i]);
 
107
          bool loaded = myLib->load();
 
108
          if (loaded)
 
109
            {
 
110
              std::cout << "Loaded " << myLib->library() << std::endl;
 
111
              name_t *pName = (name_t *) myLib->resolve("name");
 
112
              description_t *pDesc = (description_t *) myLib->resolve("description");
 
113
              version_t *pVersion = (version_t *) myLib->resolve("version");
 
114
#ifdef QGISDEBUG
 
115
              // show the values (or lack of) for each function
 
116
              if(pName){
 
117
                std::cout << "Plugin name: " << pName() << std::endl;
 
118
              }else{
 
119
                std::cout << "Plugin name not returned when queried\n";
 
120
              }
 
121
               if(pDesc){
 
122
                std::cout << "Plugin description: " << pDesc() << std::endl;
 
123
              }else{
 
124
                std::cout << "Plugin description not returned when queried\n";
 
125
              }
 
126
             if(pVersion){
 
127
                std::cout << "Plugin version: " << pVersion() << std::endl;
 
128
              }else{
 
129
                std::cout << "Plugin version not returned when queried\n";
 
130
              }
 
131
#endif
 
132
              if (pName && pDesc && pVersion)
 
133
                {
 
134
                  QCheckListItem *pl = new QCheckListItem(lstPlugins, pName(), QCheckListItem::CheckBox); //, pDesc(), pluginDir[i])
 
135
                  pl->setText(1, pVersion());
 
136
                  pl->setText(2, pDesc());
 
137
                  pl->setText(3, pluginDir[i]);
 
138
 
 
139
#ifdef QGISDEBUG
 
140
                  std::cout << "Getting an instance of the QgsPluginRegistry" << std::endl;
 
141
#endif
 
142
                  // check to see if the plugin is loaded and set the checkbox accordingly
 
143
                  QgsPluginRegistry *pRegistry = QgsPluginRegistry::instance();
 
144
                  // get the library using the plugin description
 
145
#ifdef QGISDEBUG
 
146
                  std::cout << "Getting library name from the registry" << std::endl;
 
147
#endif
 
148
                  QString libName = pRegistry->library(pName());
 
149
                  if (libName.length() > 0)
 
150
                    {
 
151
#ifdef QGISDEBUG
 
152
                      std::cout << "Found library name in the registry" << std::endl;
 
153
#endif
 
154
                      if (libName == myLib->library())
 
155
                        {
 
156
                          // set the checkbox
 
157
                          pl->setOn(true);
 
158
                        }
 
159
                    }
 
160
              } else
 
161
                {
 
162
                  std::cout << "Failed to get name, description, or type for " << myLib->library() << std::endl;
 
163
                }
 
164
          } else
 
165
            {
 
166
              std::cout << "Failed to load " << myLib->library() << std::endl;
 
167
            }
 
168
        }
 
169
    }
 
170
}
 
171
void QgsPluginManager::apply()
 
172
{
 
173
  unload();
 
174
  accept();
 
175
}
 
176
 
 
177
void QgsPluginManager::unload()
 
178
{
 
179
  QSettings settings;
 
180
#ifdef QGISDEBUG
 
181
  std::cout << "Checking for plugins to unload" << std::endl;
 
182
#endif
 
183
  QCheckListItem *lvi = (QCheckListItem *) lstPlugins->firstChild();
 
184
  while (lvi != 0)
 
185
    {
 
186
      if (!lvi->isOn())
 
187
        {
 
188
          // its off -- see if it is loaded and if so, unload it
 
189
          QgsPluginRegistry *pRegistry = QgsPluginRegistry::instance();
 
190
#ifdef QGISDEBUG
 
191
          std::cout << "Checking to see if " << lvi->text(0) << " is loaded" << std::endl;
 
192
#endif
 
193
          QgisPlugin *plugin = pRegistry->plugin(lvi->text(0));
 
194
          if (plugin)
 
195
          {
 
196
            plugin->unload();
 
197
            // remove the plugin from the registry
 
198
            pRegistry->removePlugin(lvi->text(0));
 
199
            //disable it to the qsettings file [ts]
 
200
            settings.writeEntry("/qgis/Plugins/" + lvi->text(0), false);
 
201
          }
 
202
        }
 
203
      lvi = (QCheckListItem *) lvi->nextSibling();
 
204
    }
 
205
}
 
206
 
 
207
std::vector < QgsPluginItem > QgsPluginManager::getSelectedPlugins()
 
208
{
 
209
  std::vector < QgsPluginItem > pis;
 
210
  QCheckListItem *lvi = (QCheckListItem *) lstPlugins->firstChild();
 
211
  while (lvi != 0)
 
212
    {
 
213
      if (lvi->isOn())
 
214
        {
 
215
 
 
216
          pis.push_back(QgsPluginItem(lvi->text(0), lvi->text(2), txtPluginDir->text() + "/" + lvi->text(3)));
 
217
      } else
 
218
        {
 
219
 
 
220
        }
 
221
      lvi = (QCheckListItem *) lvi->nextSibling();
 
222
    }
 
223
  return pis;
 
224
}
 
225
void QgsPluginManager::selectAll()
 
226
{
 
227
  // select all plugins
 
228
  QCheckListItem *child = dynamic_cast<QCheckListItem *>(lstPlugins->firstChild());
 
229
  while(child)
 
230
  {
 
231
    child->setOn(true);
 
232
    child = dynamic_cast<QCheckListItem *>(child->nextSibling());
 
233
  }
 
234
 
 
235
}
 
236
 
 
237
void QgsPluginManager::clearAll()
 
238
{
 
239
  // clear all selection checkboxes 
 
240
  QCheckListItem *child = dynamic_cast<QCheckListItem *>(lstPlugins->firstChild());
 
241
  while(child)
 
242
  {
 
243
    child->setOn(false);
 
244
    child = dynamic_cast<QCheckListItem *>(child->nextSibling());
 
245
  }
 
246
}