00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
#include "toolsconfigwidget.h"
00013
00014
#include <qcheckbox.h>
00015
#include <qlineedit.h>
00016
#include <qlistbox.h>
00017
#include <qtimer.h>
00018
00019
#include <kconfig.h>
00020
#include <kdeversion.h>
00021
#include <kdebug.h>
00022
#include <kdesktopfile.h>
00023
#include <kiconloader.h>
00024
#include <kmessagebox.h>
00025
#include <kurl.h>
00026
#include <kurldrag.h>
00027
00028
#include "addtooldlg.h"
00029
#include "kapplicationtree.h"
00030
00031
00032 struct ToolsConfigEntry
00033 {
00034 QString menutext;
00035 QString cmdline;
00036 bool isdesktopfile;
00037 bool captured;
00038 bool isEmpty()
const {
00039
return (
menutext.isEmpty() &&
cmdline.isEmpty() );
00040 }
00041 };
00042
00043
00044 ToolsConfigWidget::ToolsConfigWidget(
QWidget *parent,
const char *name)
00045 :
ToolsConfigWidgetBase(parent, name)
00046 {
00047
m_toolsmenuEntries.setAutoDelete(
true);
00048
m_filecontextEntries.setAutoDelete(
true);
00049
m_dircontextEntries.setAutoDelete(
true);
00050
00051 toolsmenuBox->setAcceptDrops(
true);
00052 toolsmenuBox->installEventFilter(
this);
00053 toolsmenuBox->viewport()->setAcceptDrops(
true);
00054 toolsmenuBox->viewport()->installEventFilter(
this);
00055
00056
readConfig();
00057 }
00058
00059
00060 ToolsConfigWidget::~ToolsConfigWidget()
00061 {}
00062
00063
00064 void ToolsConfigWidget::readGroup(
const QString &group,
QDict<ToolsConfigEntry> *entryDict)
00065 {
00066
KConfig *config = ToolsFactory::instance()->config();
00067 config->
setGroup(
"External Tools");
00068
QStringList list = config->
readListEntry(group);
00069
00070 QStringList::ConstIterator it;
00071
for (it = list.begin(); it != list.end(); ++it) {
00072 config->
setGroup(group +
" " + (*it));
00073
QString cmdline = config->
readPathEntry(
"CommandLine");
00074
bool isdesktopfile = config->
readBoolEntry(
"DesktopFile");
00075
bool captured = config->
readBoolEntry(
"Captured");
00076
ToolsConfigEntry *entry =
new ToolsConfigEntry;
00077 entry->
menutext = (*it);
00078 entry->
cmdline = cmdline;
00079 entry->
isdesktopfile = isdesktopfile;
00080 entry->
captured = captured;
00081 entryDict->insert(*it, entry);
00082 }
00083 }
00084
00085
00086 void ToolsConfigWidget::storeGroup(
const QString &group,
const QDict<ToolsConfigEntry> &entryDict)
00087 {
00088
KConfig *config = ToolsFactory::instance()->config();
00089
00090
QStringList list;
00091
00092
QDictIterator<ToolsConfigEntry> it(entryDict);
00093
for (; it.current(); ++it) {
00094
ToolsConfigEntry *entry = it.current();
00095 list << entry->
menutext;
00096 config->
setGroup(group +
" " + entry->
menutext);
00097
#if defined(KDE_IS_VERSION)
00098
# if KDE_IS_VERSION(3,1,3)
00099
# ifndef _KDE_3_1_3_
00100
# define _KDE_3_1_3_
00101
# endif
00102
# endif
00103
#endif
00104
#if defined(_KDE_3_1_3_)
00105
config->
writePathEntry(
"CommandLine", entry->
cmdline);
00106
#else
00107
config->
writeEntry(
"CommandLine", entry->
cmdline);
00108
#endif
00109
config->
writeEntry(
"DesktopFile", entry->
isdesktopfile);
00110 config->
writeEntry(
"Captured", entry->
captured);
00111 }
00112
00113 config->
setGroup(
"External Tools");
00114 config->
writeEntry(group, list);
00115 }
00116
00117
00118
00119 void ToolsConfigWidget::fillListBox(
QListBox *lb,
const QDict<ToolsConfigEntry> &entryDict)
00120 {
00121 lb->clear();
00122
00123
QDictIterator<ToolsConfigEntry> it(entryDict);
00124
for (; it.current(); ++it) {
00125
ToolsConfigEntry *entry = it.current();
00126
if (entry->
isdesktopfile) {
00127
KDesktopFile df(entry->
cmdline);
00128 lb->insertItem(SmallIcon(df.
readIcon()), entry->
menutext);
00129 }
else {
00130 lb->insertItem(entry->
menutext);
00131 }
00132 }
00133 }
00134
00135
00136 bool ToolsConfigWidget::addEntry(
ToolsConfigEntry *entry,
QDict<ToolsConfigEntry> *entryDict)
00137 {
00138
QString menutext = entry->
menutext;
00139
if (entryDict->find(menutext)) {
00140
delete entry;
00141 KMessageBox::sorry(
this, i18n(
"An entry with this title exists already."));
00142
return false;
00143 }
00144
00145 entryDict->insert(menutext, entry);
00146
00147
updateListBoxes();
00148
return true;
00149 }
00150
00151
00152 void ToolsConfigWidget::readConfig()
00153 {
00154
readGroup(
"Tool Menu", &
m_toolsmenuEntries);
00155
readGroup(
"File Context", &
m_filecontextEntries);
00156
readGroup(
"Dir Context", &
m_dircontextEntries);
00157
00158
updateListBoxes();
00159 }
00160
00161
00162 void ToolsConfigWidget::storeConfig()
00163 {
00164
storeGroup(
"Tool Menu",
m_toolsmenuEntries);
00165
storeGroup(
"File Context",
m_filecontextEntries);
00166
storeGroup(
"Dir Context",
m_dircontextEntries);
00167 }
00168
00169
00170 void ToolsConfigWidget::updateListBoxes()
00171 {
00172
fillListBox(toolsmenuBox,
m_toolsmenuEntries);
00173
fillListBox(filecontextBox,
m_filecontextEntries);
00174
fillListBox(dircontextBox,
m_dircontextEntries);
00175 }
00176
00177
00178 void ToolsConfigWidget::toolsmenuaddClicked()
00179 {
00180
AddToolDialog dlg(
this);
00181 dlg.setCaption(i18n(
"Add to Tools Menu"));
00182 dlg.
tree->setFocus();
00183
while (dlg.exec()) {
00184
ToolsConfigEntry *entry =
new ToolsConfigEntry;
00185 entry->
menutext = dlg.
menutextEdit->text();
00186 entry->
cmdline = dlg.
getApp().stripWhiteSpace();
00187 entry->
isdesktopfile =
false;
00188 entry->
captured = dlg.
capturedBox->isChecked();
00189
if ( entry->
isEmpty() )
00190
delete entry;
00191
else if (
addEntry(entry, &
m_toolsmenuEntries))
00192
return;
00193 }
00194 }
00195
00196
00197 void ToolsConfigWidget::toolsmenuremoveClicked()
00198 {
00199
QString menutext = toolsmenuBox->currentText();
00200
m_toolsmenuEntries.remove(menutext);
00201
updateListBoxes();
00202 }
00203
00204
00205 void ToolsConfigWidget::filecontextaddClicked()
00206 {
00207
AddToolDialog dlg(
this);
00208 dlg.setCaption(i18n(
"Add to File Context Menus"));
00209 dlg.
tree->setFocus();
00210
while (dlg.exec()) {
00211
ToolsConfigEntry *entry =
new ToolsConfigEntry;
00212 entry->
menutext = dlg.
menutextEdit->text();
00213 entry->
cmdline = dlg.
getApp().stripWhiteSpace();
00214 entry->
isdesktopfile =
false;
00215 entry->
captured = dlg.
capturedBox->isChecked();
00216
if ( entry->
isEmpty() )
00217
delete entry;
00218
00219
else if (
addEntry(entry, &
m_filecontextEntries))
00220
return;
00221 }
00222 }
00223
00224
00225 void ToolsConfigWidget::filecontextremoveClicked()
00226 {
00227
QString menutext = filecontextBox->currentText();
00228
m_filecontextEntries.remove(menutext);
00229
updateListBoxes();
00230 }
00231
00232
00233 void ToolsConfigWidget::dircontextaddClicked()
00234 {
00235
AddToolDialog dlg(
this);
00236 dlg.setCaption(i18n(
"Add to Directory Context Menus"));
00237 dlg.
tree->setFocus();
00238
if (dlg.exec()) {
00239
ToolsConfigEntry *entry =
new ToolsConfigEntry;
00240 entry->
menutext = dlg.
menutextEdit->text();
00241 entry->
cmdline = dlg.
getApp().stripWhiteSpace();
00242 entry->
isdesktopfile =
false;
00243 entry->
captured = dlg.
capturedBox->isChecked();
00244
if ( entry->
isEmpty() )
00245
delete entry;
00246
else if (
addEntry(entry, &
m_dircontextEntries))
00247
return;
00248 }
00249 }
00250
00251
00252 void ToolsConfigWidget::dircontextremoveClicked()
00253 {
00254
QString menutext = dircontextBox->currentText();
00255
m_dircontextEntries.remove(menutext);
00256
updateListBoxes();
00257 }
00258
00259
00260 bool ToolsConfigWidget::eventFilter(
QObject *o,
QEvent *e)
00261 {
00262
if (e->type() == QEvent::DragEnter || e->type() == QEvent::DragMove) {
00263
QDragMoveEvent *dme = static_cast<QDragMoveEvent*>(e);
00264
if (KURLDrag::canDecode(dme))
00265 dme->accept();
00266
return true;
00267 }
else if (e->type() == QEvent::Drop) {
00268
QDropEvent *de = static_cast<QDropEvent*>(e);
00269
KURL::List fileList;
00270
if (KURLDrag::decode(de, fileList)) {
00271 KURL::List::ConstIterator it;
00272
for (it = fileList.begin(); it != fileList.end(); ++it) {
00273
if ((*it).isLocalFile() && KDesktopFile::isDesktopFile((*it).path())) {
00274
KDesktopFile df((*it).path());
00275
ToolsConfigEntry *entry =
new ToolsConfigEntry;
00276 entry->
menutext = df.
readName();
00277 entry->
cmdline = (*it).path();
00278 entry->
isdesktopfile =
true;
00279 entry->
captured =
false;
00280
addEntry(entry, &
m_toolsmenuEntries);
00281 }
00282 }
00283 }
00284
return true;
00285 }
00286
00287
return ToolsConfigWidgetBase::eventFilter(o, e);
00288 }
00289
00290
00291 void ToolsConfigWidget::accept()
00292 {
00293
storeConfig();
00294 }
00295
#include "toolsconfigwidget.moc"