summaryrefslogtreecommitdiffstats
path: root/parts/tools/tools_part.cpp
blob: f4285546d622ccc361aa8dd0de1625c5c2ada350 (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
#include "tools_part.h"

#include <qfile.h>
#include <qpopupmenu.h>
#include <qregexp.h>
#include <qtimer.h>
#include <qvbox.h>
#include <qwhatsthis.h>

#include <kaction.h>
#include <kapplication.h>
#include <kconfig.h>
#include <kdebug.h>
#include <kdesktopfile.h>
#include <kdialogbase.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kparts/part.h>
#include <kprocess.h>
#include <ktexteditor/document.h>

#include "kdevcore.h"
#include "kdevproject.h"
#include "kdevpartcontroller.h"
#include "kdevappfrontend.h"
#include "kdevplugininfo.h"
#include "urlutil.h"
#include "configwidgetproxy.h"
#include "kdeveditorutil.h"

#include "toolsconfig.h"
#include "toolsconfigwidget.h"

#define TOOLSSETTINGS 1
#define EXTRATOOLSSETTINGS 2

static const KDevPluginInfo data("kdevtools");
K_EXPORT_COMPONENT_FACTORY( libkdevtools, ToolsFactory( data ) )

QMap<int, QString> externalToolMenuEntries;

ToolsPart::ToolsPart(QObject *parent, const char *name, const QStringList &)
	: KDevPlugin( &data, parent, name ? name : "ToolsPart")
{
  setInstance(ToolsFactory::instance());

  setXMLFile("kdevpart_tools.rc");

	m_configProxy = new ConfigWidgetProxy( core() );
	m_configProxy->createGlobalConfigPage( i18n("Tools Menu"), TOOLSSETTINGS, info()->icon() );
	m_configProxy->createGlobalConfigPage( i18n("External Tools"), EXTRATOOLSSETTINGS, info()->icon() );
	connect( m_configProxy, SIGNAL(insertConfigWidget(const KDialogBase*, QWidget*, unsigned int )),
		this, SLOT(insertConfigWidget(const KDialogBase*, QWidget*, unsigned int )) );

  connect(core(), SIGNAL(coreInitialized()), this, SLOT(updateMenu()));

  connect( core(), SIGNAL(contextMenu(QPopupMenu *, const Context *)),
           this, SLOT(contextMenu(QPopupMenu *, const Context *)) );

  // Apparently action lists can only be plugged after the
  // xmlgui client has been registered
  QTimer::singleShot(0, this, SLOT(updateToolsMenu()));
}


ToolsPart::~ToolsPart()
{
	delete m_configProxy;
}

void ToolsPart::insertConfigWidget( const KDialogBase * dlg, QWidget * page, unsigned int pagenumber )
{
	if ( pagenumber == TOOLSSETTINGS )
	{
		ToolsConfig *w = new ToolsConfig( page, "tools config widget" );
		connect(dlg, SIGNAL(okClicked()), w, SLOT(accept()));
		connect(dlg, SIGNAL(destroyed()), this, SLOT(updateMenu()));
	}
	else if ( pagenumber == EXTRATOOLSSETTINGS )
	{
		ToolsConfigWidget *w2 = new ToolsConfigWidget( page, "tools config widget" );
		connect(dlg, SIGNAL(okClicked()), w2, SLOT(accept()));
		connect(dlg, SIGNAL(destroyed()), this, SLOT(updateToolsMenu()));
	}
}

void ToolsPart::updateMenu()
{
  QPtrList<KAction> actions;

  unplugActionList("tools_list");

  KConfig *config = ToolsFactory::instance()->config();
  config->setGroup("Tools");

  QStringList list = config->readListEntry("Tools");
  for (QStringList::Iterator it = list.begin(); it != list.end(); ++it)
	{
	  QString name = *it;

	  KDesktopFile df(name, true);
	  if (df.readName().isNull())
		continue;

	  KAction *action = new KAction(df.readName(), df.readIcon(), 0,
                                        this, SLOT(slotToolActivated()), (QObject*)0, name.latin1());
	  actions.append(action);
	}

  plugActionList("tools_list", actions);
}


void ToolsPart::slotToolActivated()
{
  QString df = sender()->name();
  kapp->startServiceByDesktopPath(df);
}

void ToolsPart::startCommand(QString cmdline, bool captured, QString fileName)
{
    KTextEditor::Document * doc = dynamic_cast<KTextEditor::Document*>( partController()->activePart() );

    if ( fileName.isNull() && doc )
        fileName = doc->url().path();
    
    QString projectDirectory;
    if (project())
        projectDirectory = project()->projectDirectory();
    
    QString selection = KDevEditorUtil::currentSelection( doc );
    if ( !selection.isEmpty() )
        selection = KShellProcess::quote( selection );

    QString word = KDevEditorUtil::currentWord( doc );
    
    // This should really be checked before inserting into the popup
    if (cmdline.contains("%D") && projectDirectory.isNull())
        return;
    cmdline.replace(QRegExp("%D"), projectDirectory);
    
    if (cmdline.contains("%S") && fileName.isNull())
        return;
    cmdline.replace(QRegExp("%S"), fileName);

    if (cmdline.contains("%T") && selection.isNull())
        return;
    cmdline.replace(QRegExp("%T"), selection);

    if (cmdline.contains("%W") && word.isNull())
        return;
    cmdline.replace(QRegExp("%W"), word);

    if (captured)
    {
       if (KDevAppFrontend *appFrontend = extension<KDevAppFrontend>("KDevelop/AppFrontend"))
            appFrontend->startAppCommand(QString::QString(), cmdline, false);
    }
    else 
    {
        KShellProcess proc;
        proc << cmdline;
        proc.start(KProcess::DontCare, KProcess::NoCommunication);
    }
}


void ToolsPart::updateToolsMenu()
{
    KConfig *config = ToolsFactory::instance()->config();
    config->setGroup("External Tools");
    QStringList l = config->readListEntry("Tool Menu");


    QPtrList<KAction> actions;
    QStringList::ConstIterator it;
    for (it = l.begin(); it != l.end(); ++it) {
        QString menutext = *it;
        KConfig *config = ToolsFactory::instance()->config();
        config->setGroup("Tool Menu " + menutext);
        bool isdesktopfile = config->readBoolEntry("DesktopFile");
        KAction *action = new KAction(*it, 0,
                                      this, SLOT(toolsMenuActivated()),
                                      (QObject*) 0, menutext.utf8());
        if (isdesktopfile) {
            KDesktopFile df(config->readPathEntry("CommandLine"));
            action->setIcon(df.readIcon());
        }
        actions.append(action);
    }

    unplugActionList("tools2_list");
    plugActionList("tools2_list", actions);
}


void ToolsPart::contextMenu(QPopupMenu *popup, const Context *context)
{
    if (!context->hasType( Context::FileContext ))
        return;

    const FileContext *fcontext = static_cast<const FileContext*>(context);
    m_contextPopup = popup;
    m_contextFileName = fcontext->urls().first().path();
    externalToolMenuEntries.clear();
    
    KConfig *config = ToolsFactory::instance()->config();
    config->setGroup("External Tools");
    QStringList filecontextList = config->readListEntry("File Context");

    if (URLUtil::isDirectory(m_contextFileName)) {
        QStringList l = config->readListEntry("Dir Context");
        QStringList::ConstIterator it;
        for (it = l.begin(); it != l.end(); ++it)
            externalToolMenuEntries.insert( popup->insertItem( (*it), this, SLOT(dirContextActivated(int)) ), (*it) );
    } else {
        QStringList l = config->readListEntry("File Context");
        QStringList::ConstIterator it;
        for (it = l.begin(); it != l.end(); ++it)
            externalToolMenuEntries.insert( popup->insertItem( (*it), this, SLOT(fileContextActivated(int))  ), (*it) );
    }
}


void ToolsPart::toolsMenuActivated()
{
    QString menutext = sender()->name();
    KConfig *config = ToolsFactory::instance()->config();
    config->setGroup("Tool Menu " + menutext);
    QString cmdline = config->readPathEntry("CommandLine");
    bool isdesktopfile = config->readBoolEntry("DesktopFile");
    bool captured = config->readBoolEntry("Captured");
    kdDebug() << "tools:" << "activating " << menutext
              << "with cmdline " << cmdline
              << "and desktopfile " << isdesktopfile << endl;
    if (isdesktopfile)
        kapp->startServiceByDesktopPath(cmdline);
    else
        startCommand(cmdline, captured, QString::null);
}


void ToolsPart::fileContextActivated(int id)
{
    QString menutext = externalToolMenuEntries[ id ];

    KConfig *config = ToolsFactory::instance()->config();
    config->setGroup("File Context " + menutext);
    QString cmdline = config->readPathEntry("CommandLine");
    bool captured = config->readBoolEntry("Captured");
    kdDebug() << "filecontext:" << "activating " << menutext
              << " with cmdline " << cmdline
              << " on file " << m_contextFileName << endl;
    startCommand(cmdline, captured, m_contextFileName);
}


void ToolsPart::dirContextActivated(int id)
{
    QString menutext = externalToolMenuEntries[ id ];

    KConfig *config = ToolsFactory::instance()->config();
    config->setGroup("Dir Context " + menutext);
    QString cmdline = config->readPathEntry("CommandLine");
    bool captured = config->readBoolEntry("Captured");
    kdDebug() << "dircontext:" << "activating " << menutext
              << "with cmdline " << cmdline
              << " on directory " << m_contextFileName << endl;
    startCommand(cmdline, captured, m_contextFileName);
}

#include "tools_part.moc"