• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kget

  • sources
  • kde-4.12
  • kdenetwork
  • kget
  • transfer-plugins
  • contentfetch
dlgcontentfetchsettingwidget.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2 
3  Copyright (C) 2008 Ningyu Shi <shiningyu@gmail.com>
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 */
10 #include "dlgcontentfetchsettingwidget.h"
11 #include "dlgscriptediting.h"
12 #include "contentfetchsetting.h"
13 #include "scriptconfigadaptor.h"
14 #include "kget_export.h"
15 
16 #include <QSize>
17 
18 #include <kdialog.h>
19 #include <kdebug.h>
20 
21 KGET_EXPORT_PLUGIN_CONFIG(DlgContentFetchSettingWidget)
22 
23 DlgContentFetchSettingWidget::DlgContentFetchSettingWidget(QWidget * parent = 0, const QVariantList &args = QVariantList())
24  : KCModule(KGetFactory::componentData(), parent, args),
25  m_p_action(0)
26 {
27  ui.setupUi(this);
28  ui.newScriptButton->setIcon(KIcon("list-add"));
29  ui.removeScriptButton->setIcon(KIcon("list-remove"));
30 
31  loadContentFetchSetting();
32 
33  connect(ui.newScriptButton, SIGNAL(clicked()), this, SLOT(slotNewScript()));
34  connect(ui.editScriptButton, SIGNAL(clicked()), this, SLOT(slotEditScript()));
35  connect(ui.configureScriptButton, SIGNAL(clicked()), this, SLOT(slotConfigureScript()));
36  connect(ui.removeScriptButton, SIGNAL(clicked()), this, SLOT(slotRemoveScript()));
37  connect(ui.scriptTreeWidget,
38  SIGNAL(itemClicked(QTreeWidgetItem*,int)),
39  this, SLOT(slotCheckConfigurable(QTreeWidgetItem*,int)));
40  connect(ui.scriptTreeWidget,
41  SIGNAL(itemChanged(QTreeWidgetItem*,int)),
42  this, SLOT(slotEnableChanged(QTreeWidgetItem*,int)));
43 }
44 
45 DlgContentFetchSettingWidget::~DlgContentFetchSettingWidget()
46 {
47 }
48 
49 void DlgContentFetchSettingWidget::slotNewScript()
50 {
51  QPointer<DlgScriptEditing> dialog = new DlgScriptEditing(this);
52  if (dialog->exec())
53  {
54  addScriptItem(true, dialog->scriptPath(), dialog->scriptUrlRegexp(),
55  dialog->scriptDescription());
56  }
57  changed();
58 }
59 
60 void DlgContentFetchSettingWidget::slotEditScript()
61 {
62  QList<QTreeWidgetItem *> selectedItems =
63  ui.scriptTreeWidget->selectedItems();
64  // only edit one item at one time
65  if (selectedItems.size()!=1)
66  {
67  return;
68  }
69  QTreeWidgetItem &item = *(selectedItems[0]);
70  QPointer<DlgScriptEditing> dialog = new DlgScriptEditing(this, (QStringList() << item.toolTip(0)
71  << item.text(1) << item.text(2)));
72  if (dialog->exec())
73  {
74  if (item.toolTip(0) != dialog->scriptPath())
75  {
76  item.setText(0, QFileInfo(dialog->scriptPath()).fileName());
77  item.setToolTip(0, dialog->scriptPath());
78  changed();
79  }
80  if (item.text(1) != dialog->scriptUrlRegexp())
81  {
82  item.setText(1, dialog->scriptUrlRegexp());
83  changed();
84  }
85  if (item.text(2) != dialog->scriptDescription())
86  {
87  item.setText(2, dialog->scriptDescription());
88  changed();
89  }
90  }
91 }
92 
93 void DlgContentFetchSettingWidget::slotConfigureScript()
94 {
95  QList<QTreeWidgetItem *> selectedItems =
96  ui.scriptTreeWidget->selectedItems();
97  // only configure one item at one time
98  if (selectedItems.size()!=1)
99  {
100  return;
101  }
102  QString filename = selectedItems[0]->toolTip(0);
103  if (m_p_action)
104  {
105  delete m_p_action;
106  }
107  m_p_action = new Kross::Action(this, QString("%1_ContentFetchConfig").arg(filename));
108  // TODO add check file
109  m_p_action->setFile(filename);
110  m_p_action->addObject(this, "kgetscriptconfig",
111  Kross::ChildrenInterface::AutoConnectSignals);
112  m_p_action->trigger();
113 
114  KDialog *dialog = new KDialog(this);
115  dialog->setObjectName("configure_script");
116  dialog->setCaption(i18nc("Configure script", "Configure script"));
117  dialog->enableButtonOk(false);
118  dialog->setModal(true);
119 
120  SettingWidgetAdaptor *widget = new SettingWidgetAdaptor(dialog);
121  ScriptConfigAdaptor config;
122  emit configureScript(widget,&config);
123 
124  if (widget->findChild<QWidget*>())
125  {
126  dialog->enableButtonOk(true);
127  }
128 
129  dialog->setMainWidget(widget);
130  dialog->showButtonSeparator(true);
131  // dirty hack, add the ok/canel button size manually
132  dialog->resize(widget->size()+QSize(0,30));
133  dialog->show();
134 
135  if (dialog->exec() == QDialog::Accepted)
136  {
137  emit configurationAccepted(widget, &config);
138  }
139 
140  dialog->deleteLater();
141 }
142 
143 void DlgContentFetchSettingWidget::slotRemoveScript()
144 {
145  QList<QTreeWidgetItem *> selectedItems =
146  ui.scriptTreeWidget->selectedItems();
147 
148  foreach(QTreeWidgetItem * selectedItem, selectedItems)
149  delete(selectedItem);
150  changed();
151 }
152 
153 void DlgContentFetchSettingWidget::addScriptItem(bool enabled, const QString &path, const QString &regexp, const QString &description)
154 {
155  QTreeWidgetItem *item = new QTreeWidgetItem(QStringList() << QFileInfo(path).fileName() << regexp << description);
156  item->setToolTip(0, path);
157  item->setCheckState(0, enabled ? Qt::Checked : Qt::Unchecked);
158  ui.scriptTreeWidget->addTopLevelItem(item);
159 }
160 
161 void DlgContentFetchSettingWidget::loadContentFetchSetting()
162 {
163  ui.scriptTreeWidget->clear();//Cleanup things first
164 
165  QStringList paths = ContentFetchSetting::self()->pathList();
166  QStringList regexps = ContentFetchSetting::self()->urlRegexpList();
167  QStringList descriptions = ContentFetchSetting::self()->descriptionList();
168  QList<int> enables = ContentFetchSetting::self()->enableList();
169  // TODO: add some safety check to avoid crashing when user rc got corrputed.
170  for (int i = 0; i < paths.size(); ++i)
171  {
172  addScriptItem(bool(enables[i]), paths[i], regexps[i], descriptions[i]);
173  }
174 }
175 
176 void DlgContentFetchSettingWidget::saveContentFetchSetting()
177 {
178  kDebug(5002);
179  QStringList paths;
180  QStringList regexps;
181  QStringList descriptions;
182  QList<int> enables;
183 
184  for (int i = 0; i < ui.scriptTreeWidget->topLevelItemCount(); ++i)
185  {
186  paths.append(ui.scriptTreeWidget->topLevelItem(i)->toolTip(0));
187  regexps.append(ui.scriptTreeWidget->topLevelItem(i)->text(1));
188  descriptions.append(ui.scriptTreeWidget->topLevelItem(i)->text(2));
189  if (ui.scriptTreeWidget->topLevelItem(i)->checkState(0) == Qt::Unchecked)
190  {
191  enables.append(0);
192  }
193  else
194  {
195  enables.append(1);
196  }
197  }
198 
199  ContentFetchSetting::self()->setPathList(paths);
200  ContentFetchSetting::self()->setUrlRegexpList(regexps);
201  ContentFetchSetting::self()->setDescriptionList(descriptions);
202  ContentFetchSetting::self()->setEnableList(enables);
203 
204  ContentFetchSetting::self()->writeConfig();
205 }
206 
207 void DlgContentFetchSettingWidget::save()
208 {
209  saveContentFetchSetting();
210  // NOTICE: clean the last config script, might change in the furture
211  if (m_p_action)
212  {
213  delete m_p_action;
214  m_p_action = 0;
215  }
216 }
217 
218 void DlgContentFetchSettingWidget::load()
219 {
220  // clean the last config script
221  if (m_p_action)
222  {
223  delete m_p_action;
224  m_p_action = 0;
225  }
226  // this class is never destroyed, so reload the rc file into ui to sync
227  loadContentFetchSetting();
228 }
229 
230 void DlgContentFetchSettingWidget::slotCheckConfigurable(QTreeWidgetItem *p_item,
231  int column )
232 {
233  if (column == -1)
234  {
235  return;
236  }
237  QString filename = p_item->toolTip(0);
238  Kross::Action action(this, QString("%1_CheckConfig").arg(filename));
239  // TODO add check file
240  action.setFile(filename);
241  action.trigger();
242  if (action.functionNames().contains("configureScript"))
243  {
244  ui.configureScriptButton->setEnabled(true);
245  }
246  else
247  {
248  ui.configureScriptButton->setEnabled(false);
249  }
250 }
251 
252 void DlgContentFetchSettingWidget::slotEnableChanged(QTreeWidgetItem* p_item,
253  int column)
254 {
255  Q_UNUSED(p_item)
256  if (column != 0)
257  {
258  return;
259  }
260  changed();
261 }
DlgScriptEditing
Definition: dlgscriptediting.h:18
dlgcontentfetchsettingwidget.h
ContentFetchSetting::enableList
static QList< int > enableList()
Get List of whether the script is enabled.
Definition: contentfetchsetting.h:89
QWidget
DlgContentFetchSettingWidget::~DlgContentFetchSettingWidget
~DlgContentFetchSettingWidget()
Definition: dlgcontentfetchsettingwidget.cpp:45
ContentFetchSetting::setDescriptionList
static void setDescriptionList(const QStringList &v)
Set List of descriptions for user scripts.
Definition: contentfetchsetting.h:60
KDialog
ContentFetchSetting::self
static ContentFetchSetting * self()
Definition: contentfetchsetting.cpp:17
ContentFetchSetting::urlRegexpList
static QStringList urlRegexpList()
Get List of the Regexp to match input URL.
Definition: contentfetchsetting.h:32
ContentFetchSetting::setEnableList
static void setEnableList(const QList< int > &v)
Set List of whether the script is enabled.
Definition: contentfetchsetting.h:79
ContentFetchSetting::setUrlRegexpList
static void setUrlRegexpList(const QStringList &v)
Set List of the Regexp to match input URL.
Definition: contentfetchsetting.h:22
ContentFetchSetting::setPathList
static void setPathList(const QStringList &v)
Set List of the available search engine URLs.
Definition: contentfetchsetting.h:41
kget_export.h
DlgContentFetchSettingWidget::configurationAccepted
void configurationAccepted(QWidget *widget, QObject *configadaptor)
dlgscriptediting.h
DlgContentFetchSettingWidget::load
virtual void load()
Definition: dlgcontentfetchsettingwidget.cpp:218
contentfetchsetting.h
DlgContentFetchSettingWidget::configureScript
void configureScript(QWidget *widget, QObject *configadaptor)
scriptconfigadaptor.h
DlgContentFetchSettingWidget::save
virtual void save()
Definition: dlgcontentfetchsettingwidget.cpp:207
SettingWidgetAdaptor
Definition: dlgcontentfetchsettingwidget.h:54
ContentFetchSetting::pathList
static QStringList pathList()
Get List of the available search engine URLs.
Definition: contentfetchsetting.h:51
ContentFetchSetting::descriptionList
static QStringList descriptionList()
Get List of descriptions for user scripts.
Definition: contentfetchsetting.h:70
DlgContentFetchSettingWidget
Definition: dlgcontentfetchsettingwidget.h:20
ScriptConfigAdaptor
Definition: scriptconfigadaptor.h:20
KCModule
KGET_EXPORT_PLUGIN_CONFIG
#define KGET_EXPORT_PLUGIN_CONFIG(classname)
Definition: kget_export.h:44
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:17 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kget

Skip menu "kget"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal