summaryrefslogtreecommitdiffstats
path: root/quanta/plugins/quantaplugin.cpp
blob: 71b6fe1dbdff99bfd6cb1d175be1fc47f75a7d8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/***************************************************************************
               quantaplugin.cpp  -  Base class for quanta plugins
                             -------------------
    begin                : Mon Sep 16 2002
    copyright            : (C) 2002 by Marc Britton <[email protected]>
                           (C) 2003-2004 by Andras Mantia <[email protected]>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

/* KDE INCLUDES */
#include <kstandarddirs.h>
#include <tdelocale.h>
#include <kiconloader.h>
#include <tdeaction.h>
#include <tdeparts/componentfactory.h>
#include <tdeparts/part.h>
#include <tdelocale.h>
#include <kdockwidget.h>
#include <kdebug.h>
#include <tdemessagebox.h>

/* QT INCLUDES */
#include <tqstring.h>
#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqwidget.h>
#include <tqwidgetstack.h>
#include <tqstringlist.h>
#include <tqobject.h>

/* OTHER INCLUDES */
#include "quantaplugin.h"
#include "resource.h"
#include "quanta.h"
#include "quantaview.h"
#include "document.h"
#include "quantacommon.h"
#include "resource.h"
#include "messageoutput.h"
#include "project.h"

#include "viewmanager.h"


QuantaPlugin::QuantaPlugin()
 : m_isRunning(false)
{
 m_readOnlyPart = true;
 m_action = 0L;
 m_icon = "";
 m_part = 0L;
 m_view = 0L;
 m_pluginToolView = 0L;
 m_targetWidget = 0L;
}

QuantaPlugin::~QuantaPlugin()
{
   unload();
}

/** Sets the superficial name of the plugin */
void QuantaPlugin::setPluginName(const TQString &a_name)
{
  m_name = a_name;
  if (!m_action)
  {
    m_action = new TDEToggleAction(i18n(a_name.utf8()), 0, this, TQ_SLOT(toggle()), quantaApp->actionCollection(), a_name.ascii());
  }
  m_action->setText(a_name);
}

TQString QuantaPlugin::pluginName() const
{
  return m_name;
}

void QuantaPlugin::setLocation(const TQString &a_location)
{
  m_location = a_location;
}

TQString QuantaPlugin::location() const
{
  return m_location;
}

bool QuantaPlugin::load()
{
  if(isLoaded())
    return false;

  TQString loc = location();
  if(loc.isEmpty())
  {
    TDEStandardDirs *dirs = QuantaCommon::pluginDirs("lib");
    loc = dirs->findResource("lib", fileName());
    delete dirs;
  }
  else
  {
    loc += TQString("/") + fileName();
  }

  TQFileInfo partInfo(loc);
  TQString ow = outputWindow();
  m_targetWidget =  new TQWidget(quantaApp);
  if (m_readOnlyPart)
      m_part = KParts::ComponentFactory::createPartInstanceFromLibrary<KParts::ReadOnlyPart>(partInfo.baseName().latin1(), m_targetWidget, 0, m_targetWidget, 0 );
  else
      m_part = KParts::ComponentFactory::createPartInstanceFromLibrary<KParts::ReadWritePart>(partInfo.baseName().latin1(), m_targetWidget, 0, m_targetWidget, 0 );
  if(!m_part)
  {
    KMessageBox::error(quantaApp, i18n("<qt>The <b>%1</b> plugin could not be loaded.<br>Possible reasons are:<br>    - <b>%2</b> is not installed;<br>    - the file <i>%3</i> is not installed or it is not reachable.").arg(m_name).arg(m_name).arg(m_fileName));
    delete m_targetWidget;
    m_targetWidget = 0L;
    return false;
  }
  m_part->setName(m_name.ascii());  // for better debug output
  m_part->widget()->setName(m_name.ascii()); //needed to be able to dock the same plugin twice in separate toolviews
  m_part->widget()->setFocusPolicy(TQWidget::ClickFocus);
  quantaApp->slotNewPart(m_part, false);  // register the part in the partmanager
  return true;
}

bool QuantaPlugin::run()
{
  if(!isLoaded())
    load();

  if(isLoaded())
  {
    bool result = true;
    QuantaView *view = ViewManager::ref()->activeView();
    if (view->document())
    {
      switch (m_input)
      {
        case 1: { result = m_part->openURL(view->document()->url());
                  break;
                }
        case 2: { KURL url = view->document()->url();
                  url.setPath(url.directory());
                  url.adjustPath(1);
                  result = m_part->openURL(url);
                  break;
                }
        case 3: { KURL url;
                  if ( Project::ref()->hasProject() &&
                       (Project::ref()->contains(view->document()->url()) ||
                        view->document()->isUntitled())
                     )
                  {
                    url = Project::ref()->projectBaseURL();
                  }
                  else
                  {
                    url = view->document()->url();
                    url.setPath(url.directory());
                    url.adjustPath(1);
                  }
                  result = m_part->openURL(url);
                  break;
                }
        default: ;
      }
    } else
    {
      if (m_input == 3 && (Project::ref()->hasProject())) //open project dir
          result = m_part->openURL(Project::ref()->projectBaseURL());
    }
    if (!result)
    {
      unload(false);
      return false;
    }
    addWidget();
    setRunning(true);
    connect( m_part, TQ_SIGNAL(setStatusBarText(const TQString &)),
           quantaApp, TQ_SLOT(slotStatusMsg( const TQString & )));


    emit pluginStarted();

    return true;
  }
  return false;
}

bool QuantaPlugin::toggle()
{
  if (isLoaded())
  {
    return unload();
  } else
  {
    bool result = run();
    if (!result)
    {
      m_action->blockSignals(true);
      m_action->setChecked(false);
      m_action->blockSignals(false);
    }
    return result;
  }
}

/** Returns the file name of the plugin */
TQString QuantaPlugin::fileName() const
{
  return m_fileName;
}

/** Sets the file name of the plugin */
void QuantaPlugin::setFileName(const TQString &a_fileName)
{
  m_fileName = a_fileName;
}

/** Sets the output window */
void QuantaPlugin::setOutputWindow(const TQString &a_outputWindow)
{
  m_outputWindow = a_outputWindow;
}

/** Gets the output window */
TQString QuantaPlugin::outputWindow() const
{
  return m_outputWindow;
}

bool QuantaPlugin::validatePlugin(QuantaPlugin *a_plugin)
{
  if(a_plugin)
  {
    return QuantaPlugin::validatePluginInfo(a_plugin->pluginName(),
                         a_plugin->location(), a_plugin->fileName(),
                         a_plugin->outputWindow());
  }
  return false;
}

/** Returns true if the information of a plugin is valid, otherwise false */
bool QuantaPlugin::validatePluginInfo(const TQString & /*a_name*/,
    const TQString &a_location, const TQString &a_fileName, const TQString &/*a_outputWindow*/)
{
  bool valid = true;
  // look the file up if location is null
  if(!a_location.isEmpty())
  {
    TQString path = a_location + TQString("/") + a_fileName;

    TQFileInfo pluginFile(path);
    if(!pluginFile.exists())
      valid = false;
      /* TODO : What other tests should we perform? Permissions etc? */
  }
  else
  {
    if (valid)
    {
      TDEStandardDirs *dirs = QuantaCommon::pluginDirs("lib");
      if(dirs->findResource("lib", a_fileName).isNull())
        valid = false;
      delete dirs;
    }
  }
  return valid; // past all tests, valid
}

/** Returns whether the plugin is loaded or not */
bool QuantaPlugin::isLoaded()
{
  return m_targetWidget != 0L;
}

bool QuantaPlugin::unload(bool remove)
{
  if(!isLoaded())
    return false;

  disconnect( m_part, TQ_SIGNAL(setStatusBarText(const TQString &)),
           quantaApp, TQ_SLOT(slotStatusMsg( const TQString & )));

  delete (KParts::ReadOnlyPart*) m_part;
  m_part = 0;
  delete m_targetWidget;
  m_targetWidget = 0L;
  setRunning(false);

  emit pluginStopped();

  m_action->setChecked(false);
  if (remove)
    removeWidget();
  return true;
}

bool QuantaPlugin::isRunning() const
{
  return m_isRunning;
}

void QuantaPlugin::setRunning(bool a_enable)
{
  m_isRunning = a_enable;
}

/** Sets the plugin's standard attribute */
void QuantaPlugin::setStandard(bool isStandard)
{
  m_standard = isStandard;
}

/** Gets the plugin's standard attribute */
bool QuantaPlugin::isStandard() const
{
  return m_standard;
}

/** No descriptions */
TQString QuantaPlugin::standardName()
{
 return m_standardName;
}
/** No descriptions */
void QuantaPlugin::setStandardName(const TQString& a_stdName)
{
 m_standardName = a_stdName;
}
/** No descriptions */
TQString QuantaPlugin::icon()
{
  return m_icon;
}
/** No descriptions */
void QuantaPlugin::setIcon(const TQString& a_icon)
{
  m_icon = a_icon;
  if (m_action) m_action->setIcon(a_icon);
}

void QuantaPlugin::addWidget()
{
  TQString ow = outputWindow();
  if(ow == i18n("Separate Toolview"))
  {
    m_part->widget()->setCaption(m_name);
    m_part->widget()->setIcon(SmallIcon(m_icon));
    m_pluginToolView = quantaApp->addToolWindow(m_part->widget(), quantaApp->prevDockPosition(m_part->widget(), KDockWidget::DockBottom), quantaApp->getMainDockWidget());
    m_pluginToolView->show();
  } else
  {
    m_view = ViewManager::ref()->createView();
    m_view->addPlugin(this);
  }
}

void QuantaPlugin::removeWidget()
{
  TQString ow = outputWindow();
  if(ow == i18n("Separate Toolview"))
  {
       delete m_pluginToolView;
       m_pluginToolView = 0L;
       ViewManager::ref()->activeView()->setFocus();
  } else
  {
    ViewManager::ref()->removeView(m_view, true);
  }
}

TQWidget *QuantaPlugin::widget()
{
  if (isLoaded()) {
    return m_part->widget();
  } else {
    return NULL;
  }
}

void QuantaPlugin::plugAction(TQPopupMenu *menu)
{
  m_action->plug(menu);
}

#include "quantaplugin.moc"