Libksieve

managesievewidget.cpp
1 /*
2  Copyright (C) 2014-2020 Laurent Montel <[email protected]>
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License, version 2, as
6  published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful, but
9  WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include "managesievewidget.h"
19 #include "managesievetreeview.h"
20 #include "widgets/sievetreewidgetitem.h"
21 #include "managescriptsjob/renamescriptjob.h"
22 #include "libksieve_debug.h"
23 
24 #include <kmanagesieve/sievejob.h>
25 #include <managescriptsjob/parseuserscriptjob.h>
26 #include <managescriptsjob/generateglobalscriptjob.h>
27 #include <PimCommon/NetworkManager>
28 
29 #include "util/util_p.h"
30 
31 #include <QInputDialog>
32 #include <KStandardGuiItem>
33 #include <KLocalizedString>
34 #include <KMessageBox>
35 
36 #include <QHBoxLayout>
37 #include <QMenu>
38 #include <QTimer>
39 #include <QMetaType>
40 #include <QNetworkConfigurationManager>
41 //#define USE_RENAME_SIEVE_METHOD 1
42 using namespace KSieveUi;
43 Q_DECLARE_METATYPE(QTreeWidgetItem *)
44 
45 class KSieveUi::ManageSieveWidgetPrivate
46 {
47 public:
48  ManageSieveWidgetPrivate()
49  : mTreeView(nullptr)
50  , mClearAll(false)
51  , mBlockSignal(false)
52  {
53  }
54 
55  ~ManageSieveWidgetPrivate()
56  {
57  }
58 
59  // Maps top-level items to their child which has the radio button selection
61  ManageSieveTreeView *mTreeView = nullptr;
62  bool mClearAll : 1;
63  bool mBlockSignal : 1;
64 };
65 
66 ManageSieveWidget::ManageSieveWidget(QWidget *parent)
67  : QWidget(parent)
68  , d(new KSieveUi::ManageSieveWidgetPrivate)
69 {
70  QHBoxLayout *lay = new QHBoxLayout(this);
71  lay->setContentsMargins(0, 0, 0, 0);
72 
73  d->mTreeView = new ManageSieveTreeView(this);
74 #ifndef QT_NO_CONTEXTMENU
75  connect(d->mTreeView, &ManageSieveTreeView::customContextMenuRequested, this, &ManageSieveWidget::slotContextMenuRequested);
76 #endif
77  connect(d->mTreeView, &ManageSieveTreeView::itemDoubleClicked, this, &ManageSieveWidget::slotDoubleClicked);
78  connect(d->mTreeView, &ManageSieveTreeView::itemSelectionChanged, this, &ManageSieveWidget::slotUpdateButtons);
79  connect(d->mTreeView, &ManageSieveTreeView::itemChanged, this, &ManageSieveWidget::slotItemChanged);
80 
81  connect(PimCommon::NetworkManager::self()->networkConfigureManager(), &QNetworkConfigurationManager::onlineStateChanged, this, &ManageSieveWidget::slotSystemNetworkOnlineStateChanged);
82 
83  lay->addWidget(d->mTreeView);
84  QTimer::singleShot(0, this, &ManageSieveWidget::slotCheckNetworkStatus);
85 }
86 
87 ManageSieveWidget::~ManageSieveWidget()
88 {
89  clear();
90  delete d;
91 }
92 
93 void ManageSieveWidget::slotCheckNetworkStatus()
94 {
95  slotSystemNetworkOnlineStateChanged(PimCommon::NetworkManager::self()->networkConfigureManager()->isOnline());
96 }
97 
98 void ManageSieveWidget::slotSystemNetworkOnlineStateChanged(bool state)
99 {
100  if (state) {
101  d->mTreeView->setEnabled(true);
102  slotRefresh();
103  } else {
104  d->mTreeView->setEnabled(false);
105  d->mTreeView->setNetworkDown(false);
106  }
107 }
108 
109 ManageSieveTreeView *ManageSieveWidget::treeView() const
110 {
111  return d->mTreeView;
112 }
113 
114 void ManageSieveWidget::killAllJobs()
115 {
116  d->mClearAll = true;
118  while (it != mJobs.constEnd()) {
119  it.key()->kill();
120  ++it;
121  }
122  d->mClearAll = false;
123  mJobs.clear();
124 }
125 
126 bool ManageSieveWidget::serverHasError(QTreeWidgetItem *item) const
127 {
128  const QVariant variant = item->data(0, SIEVE_SERVER_ERROR);
129  if (variant.isValid() && variant.toBool() == true) {
130  return true;
131  }
132  return false;
133 }
134 
135 void ManageSieveWidget::slotItemChanged(QTreeWidgetItem *item, int col)
136 {
137  if (!item || d->mBlockSignal || (col != 0)) {
138  return;
139  }
140  if (!isFileNameItem(item)) {
141  return;
142  }
143  QTreeWidgetItem *parent = item->parent();
144  if ((d->mSelectedItems[parent] != item) && itemIsActived(item)) {
145  d->mSelectedItems[parent] = item;
146  changeActiveScript(parent, true);
147  } else {
148  d->mSelectedItems[parent] = item;
149  changeActiveScript(parent, false);
150  }
151 }
152 
153 void ManageSieveWidget::slotContextMenuRequested(const QPoint &p)
154 {
155  QTreeWidgetItem *item = d->mTreeView->itemAt(p);
156  if (!item) {
157  return;
158  }
159  QMenu menu;
160  if (isFileNameItem(item)) {
161  // script items:
162  menu.addAction(i18n("Edit Script..."), this, &ManageSieveWidget::slotEditScript);
163  menu.addAction(QIcon::fromTheme(QStringLiteral("edit-rename")), i18n("Rename Script..."), this, &ManageSieveWidget::slotRenameScript);
164  menu.addSeparator();
165  menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18n("Delete Script"), this, &ManageSieveWidget::slotDeleteScript);
166  if (itemIsActived(item)) {
167  menu.addSeparator();
168  menu.addAction(i18n("Deactivate Script"), this, &ManageSieveWidget::slotDeactivateScript);
169  }
170  } else if (!item->parent()) {
171  // top-levels:
172  const bool jobsListIsEmpty = mJobs.keys(item).isEmpty();
173  if (canAddNewScript(item, jobsListIsEmpty)) {
174  menu.addAction(QIcon::fromTheme(QStringLiteral("document-new")), i18n("New Script..."), this, &ManageSieveWidget::slotNewScript);
175  } else if (!jobsListIsEmpty) { //In Progress
176  menu.addAction(KStandardGuiItem::cancel().icon(), KStandardGuiItem::cancel().text(), this, &ManageSieveWidget::slotCancelFetch);
177  }
178  }
179  if (!menu.actions().isEmpty()) {
180  menu.exec(d->mTreeView->viewport()->mapToGlobal(p));
181  }
182 }
183 
184 bool ManageSieveWidget::canAddNewScript(QTreeWidgetItem *item, bool jobsListIsEmpty)
185 {
186  if (!serverHasError(item) && jobsListIsEmpty) {
187  if (item->parent()) {
188  item = item->parent();
189  }
190  if (!item) {
191  return false;
192  }
193 
194  if (!mUrls.contains(item)) {
195  return false;
196  }
197  return true;
198  }
199  return false;
200 }
201 
202 void ManageSieveWidget::slotCancelFetch()
203 {
204  QTreeWidgetItem *item = d->mTreeView->currentItem();
205  if (item) {
206  const QList<KManageSieve::SieveJob *> jobs = mJobs.keys(item);
207  for (KManageSieve::SieveJob *job : jobs) {
208  job->kill(KJob::EmitResult);
209  }
210  } else {
211  qCWarning(LIBKSIEVE_LOG) << "ManageSieveWidget::slotCancelFetch(): item is not selected";
212  }
213 }
214 
215 void ManageSieveWidget::slotNewScript()
216 {
217  QTreeWidgetItem *currentItem = d->mTreeView->currentItem();
218  if (!currentItem) {
219  return;
220  }
221  if (currentItem->parent()) {
222  currentItem = currentItem->parent();
223  }
224  if (!currentItem) {
225  return;
226  }
227 
228  if (!mUrls.contains(currentItem)) {
229  return;
230  }
231 
232  QUrl u = mUrls[currentItem];
233  if (u.isEmpty()) {
234  return;
235  }
236 
237  bool ok = false;
238  QString name = QInputDialog::getText(this, i18n("New Sieve Script"),
239  i18n("Please enter a name for the new Sieve script:"), QLineEdit::Normal,
240  i18n("unnamed"), &ok);
241  name = name.trimmed();
242  if (!ok || name.isEmpty()) {
243  if (ok && name.isEmpty()) {
244  KMessageBox::error(this, i18n("Empty name is not a valid name"), i18n("New Script"));
245  }
246  return;
247  }
248 
249  if (Util::isKep14ProtectedName(name)) {
250  KMessageBox::error(this, i18n("You cannot use protected name."), i18n("New Script"));
251  return;
252  }
253 
254  u = u.adjusted(QUrl::RemoveFilename);
255  u.setPath(u.path() + QLatin1Char('/') + name);
256 
257  QTreeWidgetItem *parentItem = currentItem;
258  if (parentItem) {
259  const int numberOfElement(parentItem->childCount());
260  for (int i = 0; i < numberOfElement; ++i) {
261  if (parentItem->child(i)->text(0) == name) {
263  this,
264  i18n("Script name already used \"%1\".", name),
265  i18n("New Script"));
266  return;
267  }
268  }
269  }
270 
271  const QStringList currentCapabilities = currentItem->data(0, SIEVE_SERVER_CAPABILITIES).toStringList();
272  const KSieveUi::SieveImapAccountSettings sieveimapaccountsettings = currentItem->data(0, SIEVE_SERVER_IMAP_SETTINGS).value<KSieveUi::SieveImapAccountSettings>();
273  const QStringList listscript = currentItem->data(0, SIEVE_SERVER_LIST_SCRIPT).toStringList();
274 
275  d->mBlockSignal = true;
276  QTreeWidgetItem *newItem = new QTreeWidgetItem(currentItem);
277  newItem->setFlags(newItem->flags() & (Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable));
278  newItem->setText(0, name);
279  newItem->setCheckState(0, Qt::Unchecked);
280  d->mBlockSignal = false;
281 
282  ScriptInfo info;
283  info.currentCapabilities = currentCapabilities;
284  info.currentUrl = u;
285  info.sieveImapAccountSettings = sieveimapaccountsettings;
286  info.scriptList = listscript;
287 
288  Q_EMIT newScript(info);
289 }
290 
291 void ManageSieveWidget::slotEditScript()
292 {
293  QTreeWidgetItem *currentItem = d->mTreeView->currentItem();
294  if (!isFileNameItem(currentItem)) {
295  return;
296  }
297  QTreeWidgetItem *parent = currentItem->parent();
298  if (!mUrls.contains(parent)) {
299  return;
300  }
301  QUrl url = mUrls[parent];
302  if (url.isEmpty()) {
303  return;
304  }
305  url = url.adjusted(QUrl::RemoveFilename);
306  url.setPath(url.path() + QLatin1Char('/') + currentItem->text(0));
307  const KSieveUi::SieveImapAccountSettings sieveimapaccountsettings = parent->data(0, SIEVE_SERVER_IMAP_SETTINGS).value<KSieveUi::SieveImapAccountSettings>();
308  const QStringList currentCapabilities = parent->data(0, SIEVE_SERVER_CAPABILITIES).toStringList();
309  const QStringList listscript = parent->data(0, SIEVE_SERVER_LIST_SCRIPT).toStringList();
310 
311  ScriptInfo info;
312  info.currentCapabilities = currentCapabilities;
313  info.currentUrl = url;
314  info.sieveImapAccountSettings = sieveimapaccountsettings;
315  info.scriptList = listscript;
316 
317  Q_EMIT editScript(info);
318 }
319 
320 void ManageSieveWidget::slotDeactivateScript()
321 {
322  QTreeWidgetItem *item = d->mTreeView->currentItem();
323  if (!isFileNameItem(item)) {
324  return;
325  }
326  QTreeWidgetItem *parent = item->parent();
327  if (itemIsActived(item)) {
328  d->mSelectedItems[parent] = item;
329  changeActiveScript(parent, false);
330  }
331 }
332 
333 void ManageSieveWidget::changeActiveScript(QTreeWidgetItem *item, bool activate)
334 {
335  if (!item) {
336  return;
337  }
338  if (!mUrls.contains(item)) {
339  return;
340  }
341  if (!d->mSelectedItems.contains(item)) {
342  return;
343  }
344  QUrl u = mUrls[item];
345  if (u.isEmpty()) {
346  return;
347  }
348 
349  if (item->data(0, SIEVE_SERVER_MODE).toInt() == Kep14EditorMode) {
350  QStringList activeScripts;
351  for (int i = 0; i < item->childCount(); ++i) {
352  QTreeWidgetItem *j = item->child(i);
353  if (itemIsActived(j)) {
354  activeScripts << j->text(0);
355  }
356  }
357  GenerateGlobalScriptJob *job = new GenerateGlobalScriptJob(u);
358  job->addUserActiveScripts(activeScripts);
359  connect(job, &GenerateGlobalScriptJob::success, this, &ManageSieveWidget::slotRefresh);
360  connect(job, &GenerateGlobalScriptJob::error, this, &ManageSieveWidget::slotGenerateGlobalScriptError);
361  job->start();
362  return;
363  }
364 
365  QTreeWidgetItem *selected = d->mSelectedItems[item];
366  if (!selected) {
367  return;
368  }
369  u = u.adjusted(QUrl::RemoveFilename);
370  u.setPath(u.path() + QLatin1Char('/') + selected->text(0));
371 
372  KManageSieve::SieveJob *job = nullptr;
373  if (activate) {
375  } else {
377  }
378  d->mBlockSignal = true;
379  connect(job, &KManageSieve::SieveJob::result, this, &ManageSieveWidget::slotRefresh);
380 }
381 
382 void ManageSieveWidget::slotGenerateGlobalScriptError(const QString &errorStr)
383 {
384  qCWarning(LIBKSIEVE_LOG) << "MManageSieveWidget::slotGenerateGlobalScriptError: error: " << errorStr;
385  slotRefresh();
386 }
387 
388 bool ManageSieveWidget::itemIsActived(QTreeWidgetItem *item) const
389 {
390  Q_ASSERT(item && item->parent());
391  return item->checkState(0) == Qt::Checked;
392 }
393 
394 bool ManageSieveWidget::isFileNameItem(QTreeWidgetItem *item) const
395 {
396  if (!item || !item->parent()) {
397  return false;
398  }
399  return item->flags() & Qt::ItemIsEnabled;
400 }
401 
402 void ManageSieveWidget::clear()
403 {
404  killAllJobs();
405  d->mSelectedItems.clear();
406  mUrls.clear();
407  d->mTreeView->clear();
408 }
409 
410 void ManageSieveWidget::slotRenameScript()
411 {
412  QTreeWidgetItem *currentItem = d->mTreeView->currentItem();
413  if (!isFileNameItem(currentItem)) {
414  return;
415  }
416  QTreeWidgetItem *parent = currentItem->parent();
417  if (!parent) {
418  return;
419  }
420 
421  if (!mUrls.contains(parent)) {
422  return;
423  }
424 
425  QUrl u = mUrls[parent];
426  if (u.isEmpty()) {
427  return;
428  }
429 
430  const QString newName = QInputDialog::getText(this, i18n("Rename Script"), i18n("Script Name:"), QLineEdit::Normal, currentItem->text(0));
431  if (newName.trimmed().isEmpty()) {
432  return;
433  }
434 
435  if (newName == currentItem->text(0)) {
436  return;
437  }
438 
439  u = u.adjusted(QUrl::RemoveFilename);
440  u.setPath(u.path() + QLatin1Char('/') + currentItem->text(0));
441 #ifdef USE_RENAME_SIEVE_METHOD
443  connect(job, &KManageSieve::SieveJob::result, this, &ManageSieveWidget::slotRenameResult);
444 #else
446  job->setOldUrl(u);
447  job->setIsActive(itemIsActived(currentItem));
448  job->setNewName(newName);
449  connect(job, &RenameScriptJob::finished, this, &ManageSieveWidget::slotRenameFinished);
450  job->start();
451 #endif
452 }
453 
454 void ManageSieveWidget::slotRenameFinished(const QUrl &oldUrl, const QUrl &newUrl, const QString &errorStr, bool success)
455 {
456  qCDebug(LIBKSIEVE_LOG) << " void ManageSieveWidget::slotRenameResult(KManageSieve::SieveJob *job, bool success)" << success;
457  if (!success) {
458  KMessageBox::error(this, errorStr, i18n("Rename Script"));
459  } else {
460  Q_EMIT scriptRenamed(oldUrl, newUrl);
461  }
462  slotRefresh();
463 }
464 
465 void ManageSieveWidget::slotRenameResult(KManageSieve::SieveJob *job, bool success)
466 {
467  Q_UNUSED(job);
468  qCDebug(LIBKSIEVE_LOG) << " void ManageSieveWidget::slotRenameResult(KManageSieve::SieveJob *job, bool success)" << success;
469  slotRefresh();
470 }
471 
472 void ManageSieveWidget::slotDeleteScript()
473 {
474  QTreeWidgetItem *currentItem = d->mTreeView->currentItem();
475  if (!isFileNameItem(currentItem)) {
476  return;
477  }
478 
479  QTreeWidgetItem *parent = currentItem->parent();
480  if (!parent) {
481  return;
482  }
483 
484  if (!mUrls.contains(parent)) {
485  return;
486  }
487 
488  QUrl u = mUrls[parent];
489  if (u.isEmpty()) {
490  return;
491  }
492 
493  if (KMessageBox::warningContinueCancel(this, i18n("Really delete script \"%1\" from the server?", currentItem->text(0)),
494  i18n("Delete Sieve Script Confirmation"),
496  != KMessageBox::Continue) {
497  return;
498  }
499 
500  u = u.adjusted(QUrl::RemoveFilename);
501  u.setPath(u.path() + QLatin1Char('/') + currentItem->text(0));
502 
504  connect(job, &KManageSieve::SieveJob::result, this, &ManageSieveWidget::slotDeleteResult);
505  Q_EMIT scriptDeleted(u);
506 }
507 
508 void ManageSieveWidget::slotDeleteResult(KManageSieve::SieveJob *job, bool success)
509 {
510  if (!success) {
511  KMessageBox::error(this, i18n("Deleting the script failed.\n"
512  "The server responded:\n%1", job->errorString()), i18n("Sieve Error"));
513  }
514  slotRefresh();
515 }
516 
517 void ManageSieveWidget::slotRefresh()
518 {
519  d->mBlockSignal = true;
520  clear();
521  const bool noImapFound = refreshList();
522  slotUpdateButtons();
523  d->mTreeView->setNoImapFound(noImapFound);
524  if (!noImapFound) {
525  d->mBlockSignal = false;
526  }
527  Q_EMIT serverSieveFound(!noImapFound);
528 }
529 
530 void ManageSieveWidget::slotUpdateButtons()
531 {
532  Q_EMIT updateButtons(d->mTreeView->currentItem());
533 }
534 
535 void ManageSieveWidget::slotGotList(KManageSieve::SieveJob *job, bool success, const QStringList &listScript, const QString &activeScript)
536 {
537  //qCDebug(LIBKSIEVE_LOG) << "void ManageSieveWidget::slotGotList(KManageSieve::SieveJob *job, bool success, const QStringList &listScript, const QString &activeScript) success: " << success
538  // << " listScript" << listScript;
539  if (d->mClearAll) {
540  return;
541  }
542  //qCDebug(LIBKSIEVE_LOG) << " After mClear All";
543  QTreeWidgetItem *parent = mJobs[job];
544  //qCDebug(LIBKSIEVE_LOG) << " parent " << parent;
545  if (!parent) {
546  return;
547  }
548  (static_cast<SieveTreeWidgetItem *>(parent))->stopAnimation();
549 
550  mJobs.remove(job);
551  if (!success) {
552  d->mBlockSignal = false;
553  parent->setData(0, SIEVE_SERVER_ERROR, true);
554  QTreeWidgetItem *item = new QTreeWidgetItem(parent);
555  item->setText(0, i18n("Failed to fetch the list of scripts"));
556  item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
557  d->mTreeView->expandItem(parent);
558  return;
559  }
560 
561  d->mBlockSignal = true; // don't trigger slotItemChanged
562  for (const QString &script : listScript) {
563  //Hide protected name.
564  if (Util::isKep14ProtectedName(script)) {
565  continue;
566  }
567  QTreeWidgetItem *item = new QTreeWidgetItem(parent);
568  item->setFlags(item->flags() & (Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable));
569 
570  item->setText(0, script);
571  const bool isActive = (script == activeScript);
572  item->setCheckState(0, isActive ? Qt::Checked : Qt::Unchecked);
573  if (isActive) {
574  d->mSelectedItems[parent] = item;
575  }
576  }
577  d->mBlockSignal = false;
578 
579  bool hasKep14EditorMode = Util::hasKep14Support(job->sieveCapabilities(), listScript, activeScript);
580  if (hasKep14EditorMode) {
581  QUrl u = mUrls[parent];
582  u = u.adjusted(QUrl::RemoveFilename);
583  u.setPath(u.path() + QLatin1Char('/') + QStringLiteral("USER"));
584  ParseUserScriptJob *parseJob = new ParseUserScriptJob(u, this);
585  parseJob->setAutoDelete(true);
586  parseJob->setProperty("parentItem", QVariant::fromValue<QTreeWidgetItem *>(parent));
587  connect(parseJob, &ParseUserScriptJob::finished, this, &ManageSieveWidget::setActiveScripts);
588  parseJob->start();
589  (static_cast<SieveTreeWidgetItem *>(parent))->startAnimation();
590  } else if (Util::hasKep14CapabilitySupport(job->sieveCapabilities())) { //We don't have user script but server support kep14
591  hasKep14EditorMode = true;
592  }
593 
594  parent->setData(0, SIEVE_SERVER_CAPABILITIES, job->sieveCapabilities());
595  parent->setData(0, SIEVE_SERVER_ERROR, false);
596  parent->setData(0, SIEVE_SERVER_MODE, hasKep14EditorMode ? Kep14EditorMode : NormalEditorMode);
597  parent->setData(0, SIEVE_SERVER_IMAP_SETTINGS, QVariant::fromValue(job->property("sieveimapaccountsettings").value<KSieveUi::SieveImapAccountSettings>()));
598  parent->setData(0, SIEVE_SERVER_LIST_SCRIPT, listScript);
599  d->mTreeView->expandItem(parent);
600 }
601 
602 void ManageSieveWidget::setActiveScripts(ParseUserScriptJob *job)
603 {
604  QTreeWidgetItem *parent = job->property("parentItem").value<QTreeWidgetItem *>();
605  if (!parent) {
606  return;
607  }
608  (static_cast<SieveTreeWidgetItem *>(parent))->stopAnimation();
609 
610  if (!job->error().isEmpty()) {
611  qCWarning(LIBKSIEVE_LOG) << job->error();
612  return;
613  }
614 
615  d->mBlockSignal = true; // don't trigger slotItemChanged
616  const QStringList activeScriptList = job->activeScriptList();
617  QStringList scriptOrder = activeScriptList;
619 
620  const int children = parent->childCount();
621  for (int i = 0; i < children; ++i) {
622  QTreeWidgetItem *item = parent->takeChild(0);
623  scriptMap.insert(item->text(0), item);
624  const bool isActive = activeScriptList.contains(item->text(0));
625  item->setCheckState(0, isActive ? Qt::Checked : Qt::Unchecked);
626  if (!isActive) {
627  scriptOrder << item->text(0);
628  }
629  }
630 
631  for (const QString &scriptName : qAsConst(scriptOrder)) {
632  parent->addChild(scriptMap[scriptName]);
633  }
634 
635  d->mBlockSignal = false;
636 }
637 
638 void ManageSieveWidget::slotDoubleClicked(QTreeWidgetItem *item)
639 {
640  if (!isFileNameItem(item)) {
641  return;
642  }
643  slotEditScript();
644 }
645 
646 void ManageSieveWidget::enableDisableActions(bool &newScriptAction, bool &editScriptAction, bool &deleteScriptAction, bool &desactivateScriptAction)
647 {
648  QTreeWidgetItem *item = d->mTreeView->currentItem();
649 
650  bool enabled = true;
651  if (!item) {
652  enabled = false;
653  } else if (!item->parent() && !mUrls.contains(item)) {
654  enabled = false;
655  }
656 
657  if (!enabled) {
658  newScriptAction = false;
659  editScriptAction = false;
660  deleteScriptAction = false;
661  desactivateScriptAction = false;
662  } else {
663  if (serverHasError(item) || !mJobs.keys(item).isEmpty()) {
664  newScriptAction = false;
665  } else {
666  newScriptAction = mUrls.contains(item);
667  }
668  enabled = isFileNameItem(item);
669  editScriptAction = enabled;
670  deleteScriptAction = enabled;
671  desactivateScriptAction = enabled && itemIsActived(item);
672  }
673 }
void customContextMenuRequested(const QPoint &pos)
void setContentsMargins(int left, int top, int right, int bottom)
void itemChanged(QTreeWidgetItem *item, int column)
Qt::CheckState checkState(int column) const const
static SieveJob * activate(const QUrl &url)
Activates the script with the given sieve url.
Definition: sievejob.cpp:390
QTreeWidgetItem * child(int index) const const
The ManageSieveTreeView class.
QMap::const_iterator constBegin() const const
const QObjectList & children() const const
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
void result(KManageSieve::SieveJob *job, bool success, const QString &script, bool active)
This signal is emitted for all kind of jobs when they have finished.
T value() const const
static SieveJob * del(const QUrl &url)
Deletes the script with the given sieve url.
Definition: sievejob.cpp:337
virtual void setData(int column, int role, const QVariant &value)
void onlineStateChanged(bool isOnline)
virtual QVariant data(int column, int role) const const
void clear()
KGuiItem cancel()
bool isEmpty() const const
QAction * addAction(const QString &text)
void clear()
QString name(StandardShortcut id)
KGuiItem del()
void setPath(const QString &path, QUrl::ParsingMode mode)
void setFlags(Qt::ItemFlags flags)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
QVariant property(const char *name) const const
ButtonCode warningContinueCancel(QWidget *parent, const QString &text, const QString &caption=QString(), const KGuiItem &buttonContinue=KStandardGuiItem::cont(), const KGuiItem &buttonCancel=KStandardGuiItem::cancel(), const QString &dontAskAgainName=QString(), Options options=Notify)
int toInt(bool *ok) const const
bool isEmpty() const const
bool isEmpty() const const
QString trimmed() const const
QMap::const_iterator constEnd() const const
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
QString path(QUrl::ComponentFormattingOptions options) const const
QStringList sieveCapabilities() const
Returns the sieve capabilities of the IMAP server.
Definition: sievejob.cpp:264
QAction * addSeparator()
QTreeWidgetItem * currentItem() const const
QAction * exec()
The SieveTreeWidgetItem class.
void addChild(QTreeWidgetItem *child)
The RenameScriptJob class.
QString getText(QWidget *parent, const QString &title, const QString &label, QLineEdit::EchoMode mode, const QString &text, bool *ok, Qt::WindowFlags flags, Qt::InputMethodHints inputMethodHints)
QString errorString() const
A human-readable error message.
Definition: sievejob.cpp:278
QVariant fromValue(const T &value)
QTreeWidgetItem * parent() const const
QAbstractItemView::State state() const const
const Key key(const T &value, const Key &defaultKey) const const
QString i18n(const char *text, const TYPE &arg...)
void itemSelectionChanged()
void setCheckState(int column, Qt::CheckState state)
Qt::ItemFlags flags() const const
QStringList toStringList() const const
QUrl adjusted(QUrl::FormattingOptions options) const const
void setText(int column, const QString &text)
bool toBool() const const
bool isValid() const const
The SieveImapAccountSettings class.
QIcon fromTheme(const QString &name)
QMap::iterator insert(const Key &key, const T &value)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QList< QAction * > actions() const const
QObject * parent() const const
A job to manage sieve scripts.
Definition: sievejob.h:41
int childCount() const const
QString text(int column) const const
static SieveJob * deactivate(const QUrl &url)
Deactivates the script with the given sieve url.
Definition: sievejob.cpp:350
void itemDoubleClicked(QTreeWidgetItem *item, int column)
static SieveJob * rename(const QUrl &url, const QString &newName)
Rename the script with the given sieve url and new name newName.
Definition: sievejob.cpp:362
Q_EMITQ_EMIT
QTreeWidgetItem * takeChild(int index)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun May 24 2020 22:46:40 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.