summaryrefslogtreecommitdiffstats
path: root/ksquirrel/sq_externaltool.cpp
blob: adc0fdace2dd3f68bd5a30ed98a327f453996f6a (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
/***************************************************************************
                          sq_externaltool.cpp  -  description
                             -------------------
    begin                : ??? ??? 12 2004
    copyright            : (C) 2004 by Baryshev Dmitry
    email                : ksquirrel.iv@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <tqfile.h>

#include <kstringhandler.h>
#include <klocale.h>
#include <kicontheme.h>
#include <kstandarddirs.h>
#include <kprocess.h>
#include <kmessagebox.h>

#include "sq_iconloader.h"
#include "sq_externaltool.h"
#include "sq_popupmenu.h"
#include "sq_config.h"

SQ_ExternalTool * SQ_ExternalTool::m_instance = 0;

Tool::Tool()
{}

Tool::Tool(const TQString &pix, const TQString &nam, const TQString &com)
{
    icon = pix;
    name = nam;
    command = com;
}

SQ_ExternalTool::SQ_ExternalTool(TQObject *tqparent) : TQObject(tqparent), TQValueVector<Tool>()
{
    m_instance = this;
    menu = new SQ_PopupMenu(0, "External tools");

    connect(menu, TQT_SIGNAL(aboutToShow()), this, TQT_SLOT(slotAboutToShowMenu()));
    connect(menu, TQT_SIGNAL(activated(int)), this, TQT_SLOT(slotActivateTool(int)));

    TQString str, tmp;

    SQ_Config::instance()->setGroup("External tools");

    TQStringList names = SQ_Config::instance()->readListEntry("names");
    TQStringList commands = SQ_Config::instance()->readListEntry("commands");
    TQStringList icons = SQ_Config::instance()->readListEntry("icons");

    for(TQStringList::iterator it_n = names.begin(),it_c = commands.begin(),it_i = icons.begin();
            it_n != names.end() || it_c != commands.end() || it_i != icons.end();
            ++it_n, ++it_c, ++it_i)
    {
        append(Tool(*it_i, *it_n, *it_c));
    }
}

SQ_ExternalTool::~SQ_ExternalTool()
{
    delete menu;
}

TQString SQ_ExternalTool::toolPixmap(const int i)
{
    return (*this)[i].icon;
}

TQString SQ_ExternalTool::toolName(const int i)
{
    return (*this)[i].name;
}

TQString SQ_ExternalTool::toolCommand(const int i)
{
    return (*this)[i].command;
}

/*
 *  Recreate current popop menu.
 */
SQ_PopupMenu* SQ_ExternalTool::newPopupMenu()
{
    int id;

    // clear menu
    menu->clear();

    menu->insertTitle(i18n("No file selected"));

    // construct new menu
    for(unsigned int i = 0;i < count();i++)
    {
        id = menu->insertItem(SQ_IconLoader::instance()->loadIcon(toolPixmap(i), KIcon::Desktop, KIcon::SizeSmall), toolName(i));
        menu->setItemParameter(id, i);
    }

    return menu;
}

/*
 *  Get current popup menu.
 */
SQ_PopupMenu* SQ_ExternalTool::constPopupMenu() const
{
    return menu;
}

/*
 *  Write tools to config file
 */
void SQ_ExternalTool::writeEntries()
{
    // no tools ?
    if(!count()) return;

    TQString num;

    // delete old group with old items
    SQ_Config::instance()->deleteGroup("External tools");
    SQ_Config::instance()->setGroup("External tools");
    TQStringList names, icons, commands;

    // write items in config file
    for(TQValueVector<Tool>::iterator it = begin();it != end();++it)
    {
        names.append((*it).name);
        icons.append((*it).icon);
        commands.append((*it).command);
    }

    SQ_Config::instance()->writeEntry("names", names);
    SQ_Config::instance()->writeEntry("commands", commands);
    SQ_Config::instance()->writeEntry("icons", icons);
}

/*
 *  Invoked, when user executed popup menu with external tools.
 *  This slot will do some useful stuff.
 */
void SQ_ExternalTool::slotAboutToShowMenu()
{
    if(!items.count())
    {
        menu->changeTitle(i18n("No file selected"));
        return;
    }

    KFileItem *item = items.first();

    if(!item)
    {
        menu->changeTitle(i18n("No file selected"));
        return;
    }

    // make title shorter
    TQString file = KStringHandler::rsqueeze(item->name(), 30);

    // finally, change title
    TQString final = (items.count() == 1 || items.count() == 0) ? file : (file + TQString::tqfromLatin1(" (+%1)").tqarg(items.count()-1));
    menu->changeTitle(final);
}

void SQ_ExternalTool::slotActivateTool(int id)
{
    KURL::List list;

    if(items.isEmpty()) return;

    int index = menu->itemParameter(id);

    KFileItem *f = items.first();

    while(f)
    {
        list.append(f->url());
        f = items.next();
    }

    items.clear();

    if(list.empty()) return;

    KShellProcess proc;

    // get appropriate tool
    Tool *tool = &at(index);
    TQString comm = tool->command;

    int per_f = comm.contains("%f");
    int per_F = comm.contains("%F");

    // %f = single file
    // %F = multiple files
    if(per_f && per_F)
    {
        KMessageBox::error(0, i18n("Command cannot contain both \"%f\" and \"%F\""), i18n("Error processing command"));
        return;
    }
    else if(!per_f && !per_F)
    {
        KMessageBox::error(0, i18n("Command should contain \"%f\" or \"%F\""), i18n("Error processing command"));
        return;
    }
    else if(per_f)
    {
        KURL u = list.first();
        comm.replace("%f", KShellProcess::quote(u.isLocalFile() ? u.path() : u.prettyURL()));
        proc << comm;
    }
    else
    {
        TQString files;

        KURL::List::iterator itEnd = list.end();

        for(KURL::List::iterator it = list.begin();it != itEnd;++it)
        {
            files.append(KShellProcess::quote((*it).isLocalFile() ? (*it).path() : (*it).prettyURL()));
            files.append(" ");
        }

        comm.replace("%F", files);
        proc << comm;
    }

    // start process
    proc.start(KProcess::DontCare);
}

#include "sq_externaltool.moc"