Changeset View
Changeset View
Standalone View
Standalone View
libksieve/ksieveui/widgets/managesievewidget.cpp
Show All 15 Lines | |||||
*/ | */ | ||||
#include "managesievewidget.h" | #include "managesievewidget.h" | ||||
#include "managesievetreeview.h" | #include "managesievetreeview.h" | ||||
#include "widgets/sievetreewidgetitem.h" | #include "widgets/sievetreewidgetitem.h" | ||||
#include <kmanagesieve/sievejob.h> | #include <kmanagesieve/sievejob.h> | ||||
#include <managescriptsjob/parseuserscriptjob.h> | |||||
#include <managescriptsjob/generateglobalscriptjob.h> | |||||
#include <util/util.h> | |||||
#include <KInputDialog> | #include <KInputDialog> | ||||
#include <KStandardGuiItem> | #include <KStandardGuiItem> | ||||
#include <KLocalizedString> | #include <KLocalizedString> | ||||
#include <KMessageBox> | #include <KMessageBox> | ||||
#include <QHBoxLayout> | #include <QHBoxLayout> | ||||
#include <QMenu> | #include <QMenu> | ||||
#include <QTimer> | #include <QTimer> | ||||
#include <QDebug> | #include <QDebug> | ||||
#include <qmetatype.h> | |||||
using namespace KSieveUi; | using namespace KSieveUi; | ||||
Q_DECLARE_METATYPE(QTreeWidgetItem*) | |||||
ManageSieveWidget::ManageSieveWidget(QWidget *parent) | ManageSieveWidget::ManageSieveWidget(QWidget *parent) | ||||
: QWidget(parent), | : QWidget(parent), | ||||
mClearAll( false ), | mClearAll( false ), | ||||
mBlockSignal( false ) | mBlockSignal( false ) | ||||
{ | { | ||||
QHBoxLayout *lay = new QHBoxLayout; | QHBoxLayout *lay = new QHBoxLayout; | ||||
lay->setMargin(0); | lay->setMargin(0); | ||||
▲ Show 20 Lines • Show All 125 Lines • ▼ Show 20 Lines | void ManageSieveWidget::slotNewScript() | ||||
bool ok = false; | bool ok = false; | ||||
const QString name = KInputDialog::getText( i18n( "New Sieve Script" ), | const QString name = KInputDialog::getText( i18n( "New Sieve Script" ), | ||||
i18n( "Please enter a name for the new Sieve script:" ), | i18n( "Please enter a name for the new Sieve script:" ), | ||||
i18n( "unnamed" ), &ok, this ); | i18n( "unnamed" ), &ok, this ); | ||||
if ( !ok || name.isEmpty() ) | if ( !ok || name.isEmpty() ) | ||||
return; | return; | ||||
if (isProtectedName(name.toLower())) { | if (Util::isKep14ProtectedName(name)) { | ||||
KMessageBox::error(this, i18n("You cannot use protected name."), i18n("New Script")); | KMessageBox::error(this, i18n("You cannot use protected name."), i18n("New Script")); | ||||
return; | return; | ||||
} | } | ||||
u.setFileName( name ); | u.setFileName( name ); | ||||
QTreeWidgetItem * parentItem = currentItem; | QTreeWidgetItem * parentItem = currentItem; | ||||
if (parentItem) { | if (parentItem) { | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | if ( !item ) | ||||
return; | return; | ||||
if ( !mUrls.count( item ) ) | if ( !mUrls.count( item ) ) | ||||
return; | return; | ||||
if ( !mSelectedItems.count( item ) ) | if ( !mSelectedItems.count( item ) ) | ||||
return; | return; | ||||
KUrl u = mUrls[item]; | KUrl u = mUrls[item]; | ||||
if ( u.isEmpty() ) | if ( u.isEmpty() ) | ||||
return; | return; | ||||
if (item->data(0, SIEVE_SERVER_MODE).toInt() == Kep14EditorMode) { | |||||
QStringList activeScripts; | |||||
for(int i=0; i < item->childCount(); i++) { | |||||
QTreeWidgetItem *j = item->child(i); | |||||
if (itemIsActived(j)) { | |||||
activeScripts << j->text(0); | |||||
} | |||||
} | |||||
GenerateGlobalScriptJob *job = new GenerateGlobalScriptJob(u); | |||||
job->addUserActiveScripts(activeScripts); | |||||
connect( job, SIGNAL(success()), SLOT(slotRefresh())); | |||||
connect( job, SIGNAL(error(QString)), SLOT(slotRefresh())); | |||||
job->start(); | |||||
return; | |||||
} | |||||
QTreeWidgetItem* selected = mSelectedItems[item]; | QTreeWidgetItem* selected = mSelectedItems[item]; | ||||
if ( !selected ) | if ( !selected ) | ||||
return; | return; | ||||
u.setFileName( selected->text(0) ); | u.setFileName( selected->text(0) ); | ||||
KManageSieve::SieveJob * job; | KManageSieve::SieveJob * job; | ||||
if ( activate ) | if ( activate ) | ||||
job = KManageSieve::SieveJob::activate( u ); | job = KManageSieve::SieveJob::activate( u ); | ||||
else | else | ||||
job = KManageSieve::SieveJob::deactivate( u ); | job = KManageSieve::SieveJob::deactivate( u ); | ||||
mBlockSignal = true; | mBlockSignal = true; | ||||
connect( job, SIGNAL(result(KManageSieve::SieveJob*,bool,QString,bool)), | connect( job, SIGNAL(result(KManageSieve::SieveJob*,bool,QString,bool)), | ||||
this, SLOT(slotRefresh()) ); | this, SLOT(slotRefresh()) ); | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | if ( KMessageBox::warningContinueCancel( this, i18n( "Really delete script \"%1\" from the server?", u.fileName() ), | ||||
!= KMessageBox::Continue ) | != KMessageBox::Continue ) | ||||
return; | return; | ||||
KManageSieve::SieveJob * job = KManageSieve::SieveJob::del( u ); | KManageSieve::SieveJob * job = KManageSieve::SieveJob::del( u ); | ||||
connect( job, SIGNAL(result(KManageSieve::SieveJob*,bool,QString,bool)), | connect( job, SIGNAL(result(KManageSieve::SieveJob*,bool,QString,bool)), | ||||
this, SLOT(slotRefresh()) ); | this, SLOT(slotRefresh()) ); | ||||
Q_EMIT scriptDeleted(u); | Q_EMIT scriptDeleted(u); | ||||
} | } | ||||
bool ManageSieveWidget::isProtectedName(const QString &name) | |||||
{ | |||||
if (name == QLatin1String("master") || | |||||
name == QLatin1String("user") || | |||||
name == QLatin1String("management")) { | |||||
return true; | |||||
} | |||||
return false; | |||||
} | |||||
void ManageSieveWidget::slotRefresh() | void ManageSieveWidget::slotRefresh() | ||||
{ | { | ||||
mBlockSignal = true; | mBlockSignal = true; | ||||
clear(); | clear(); | ||||
const bool noImapFound = refreshList(); | const bool noImapFound = refreshList(); | ||||
slotUpdateButtons(); | slotUpdateButtons(); | ||||
mTreeView->setNoImapFound(noImapFound); | mTreeView->setNoImapFound(noImapFound); | ||||
if (!noImapFound) | if (!noImapFound) | ||||
mBlockSignal = false; | mBlockSignal = false; | ||||
} | } | ||||
void ManageSieveWidget::slotUpdateButtons() | void ManageSieveWidget::slotUpdateButtons() | ||||
{ | { | ||||
Q_EMIT updateButtons(mTreeView->currentItem()); | Q_EMIT updateButtons(mTreeView->currentItem()); | ||||
} | } | ||||
void ManageSieveWidget::slotGotList(KManageSieve::SieveJob *job, bool success, const QStringList &listScript, const QString &activeScript) | void ManageSieveWidget::slotGotList(KManageSieve::SieveJob *job, bool success, const QStringList &listScript, const QString &activeScript) | ||||
{ | { | ||||
qDebug()<<"void ManageSieveWidget::slotGotList(KManageSieve::SieveJob *job, bool success, const QStringList &listScript, const QString &activeScript) success: "<<success<<" listScript"<<listScript; | qDebug()<<"void ManageSieveWidget::slotGotList(KManageSieve::SieveJob *job, bool success, const QStringList &listScript, const QString &activeScript) success: "<<success<<" listScript"<<listScript; | ||||
if (mClearAll) | if (mClearAll) | ||||
return; | return; | ||||
qDebug()<<" After mClear All"; | |||||
QTreeWidgetItem * parent = mJobs[job]; | QTreeWidgetItem * parent = mJobs[job]; | ||||
qDebug()<<" parent "<<parent; | |||||
if ( !parent ) | if ( !parent ) | ||||
return; | return; | ||||
(static_cast<SieveTreeWidgetItem*>(parent))->stopAnimation(); | (static_cast<SieveTreeWidgetItem*>(parent))->stopAnimation(); | ||||
mJobs.remove( job ); | mJobs.remove( job ); | ||||
if (!success) { | if (!success) { | ||||
mBlockSignal = false; | mBlockSignal = false; | ||||
parent->setData( 0, SIEVE_SERVER_ERROR, true ); | parent->setData( 0, SIEVE_SERVER_ERROR, true ); | ||||
QTreeWidgetItem * item = new QTreeWidgetItem( parent ); | QTreeWidgetItem * item = new QTreeWidgetItem( parent ); | ||||
item->setText( 0, i18n( "Failed to fetch the list of scripts" ) ); | item->setText( 0, i18n( "Failed to fetch the list of scripts" ) ); | ||||
item->setFlags( item->flags() & ~Qt::ItemIsEnabled ); | item->setFlags( item->flags() & ~Qt::ItemIsEnabled ); | ||||
mTreeView->expandItem( parent ); | mTreeView->expandItem( parent ); | ||||
return; | return; | ||||
} | } | ||||
mBlockSignal = true; // don't trigger slotItemChanged | mBlockSignal = true; // don't trigger slotItemChanged | ||||
Q_FOREACH (const QString &script, listScript) { | Q_FOREACH (const QString &script, listScript) { | ||||
//Hide protected name. | //Hide protected name. | ||||
const QString lowerScript(script.toLower()); | if (Util::isKep14ProtectedName(script)) | ||||
if (isProtectedName(lowerScript)) | |||||
continue; | continue; | ||||
QTreeWidgetItem* item = new QTreeWidgetItem( parent ); | QTreeWidgetItem* item = new QTreeWidgetItem( parent ); | ||||
item->setFlags(item->flags() & (Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable)); | item->setFlags(item->flags() & (Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable)); | ||||
item->setText(0, script); | item->setText(0, script); | ||||
const bool isActive = (script == activeScript); | const bool isActive = (script == activeScript); | ||||
item->setCheckState(0, isActive ? Qt::Checked : Qt::Unchecked); | item->setCheckState(0, isActive ? Qt::Checked : Qt::Unchecked); | ||||
if ( isActive ) { | if ( isActive ) { | ||||
mSelectedItems[parent] = item; | mSelectedItems[parent] = item; | ||||
} | } | ||||
} | } | ||||
mBlockSignal = false; | mBlockSignal = false; | ||||
qDebug()<<" LOAD"; | const bool hasKep14EditorMode = Util::hasKep14Support(job->sieveCapabilities(), listScript, activeScript); | ||||
const bool hasIncludeCapability = job->sieveCapabilities().contains(QLatin1String("include")); | if (hasKep14EditorMode) { | ||||
const bool hasUserActiveScript = (activeScript.toLower() == QLatin1String("USER")); | KUrl u = mUrls[parent]; | ||||
QStringList mUserActiveScriptList; | u.setFileName(QLatin1String("USER")); | ||||
if (hasUserActiveScript && hasIncludeCapability) { | ParseUserScriptJob *parseJob = new ParseUserScriptJob(u); | ||||
//TODO parse file. | parseJob->setProperty(QLatin1String("parentItem").latin1(), QVariant::fromValue<QTreeWidgetItem*>(parent)); | ||||
connect(parseJob, SIGNAL(finished(ParseUserScriptJob*)), SLOT(setActiveScripts(ParseUserScriptJob*))); | |||||
parseJob->start(); | |||||
(static_cast<SieveTreeWidgetItem*>(parent))->startAnimation(); | |||||
} | } | ||||
parent->setData( 0, SIEVE_SERVER_CAPABILITIES, job->sieveCapabilities() ); | parent->setData( 0, SIEVE_SERVER_CAPABILITIES, job->sieveCapabilities() ); | ||||
parent->setData( 0, SIEVE_SERVER_ERROR, false ); | parent->setData( 0, SIEVE_SERVER_ERROR, false ); | ||||
parent->setData( 0, SIEVE_SERVER_MODE, hasIncludeCapability ? Kep14EditorMode : NormalEditorMode); | parent->setData( 0, SIEVE_SERVER_MODE, hasKep14EditorMode ? Kep14EditorMode : NormalEditorMode); | ||||
mTreeView->expandItem( parent ); | mTreeView->expandItem( parent ); | ||||
} | } | ||||
void ManageSieveWidget::setActiveScripts(ParseUserScriptJob *job) | |||||
{ | |||||
QTreeWidgetItem * parent = job->property(QLatin1String("parentItem").latin1()).value<QTreeWidgetItem*>(); | |||||
if ( !parent ) { | |||||
return; | |||||
} | |||||
(static_cast<SieveTreeWidgetItem*>(parent))->stopAnimation(); | |||||
if (!job->error().isEmpty()) { | |||||
qWarning() << job->error(); | |||||
return; | |||||
} | |||||
mBlockSignal = true; // don't trigger slotItemChanged | |||||
const QStringList activeScriptList = job->activeScriptList(); | |||||
QStringList scriptOrder = activeScriptList; | |||||
QMap<QString, QTreeWidgetItem*> scriptMap; | |||||
const int children = parent->childCount(); | |||||
for(int i=0; i < children; i++) { | |||||
QTreeWidgetItem *item = parent->takeChild(0); | |||||
scriptMap.insert(item->text(0), item); | |||||
const bool isActive = activeScriptList.contains(item->text(0)); | |||||
item->setCheckState(0, isActive ? Qt::Checked : Qt::Unchecked); | |||||
if (!isActive) { | |||||
scriptOrder << item->text(0); | |||||
} | |||||
} | |||||
foreach(const QString &scriptName, scriptOrder) { | |||||
parent->addChild(scriptMap[scriptName]); | |||||
} | |||||
mBlockSignal = false; | |||||
} | |||||
void ManageSieveWidget::slotDoubleClicked( QTreeWidgetItem * item ) | void ManageSieveWidget::slotDoubleClicked( QTreeWidgetItem * item ) | ||||
{ | { | ||||
if ( !isFileNameItem( item ) ) | if ( !isFileNameItem( item ) ) | ||||
return; | return; | ||||
slotEditScript(); | slotEditScript(); | ||||
} | } | ||||
void ManageSieveWidget::enableDisableActions(bool &newScriptAction, bool &editScriptAction, bool &deleteScriptAction, bool &desactivateScriptAction) | void ManageSieveWidget::enableDisableActions(bool &newScriptAction, bool &editScriptAction, bool &deleteScriptAction, bool &desactivateScriptAction) | ||||
Show All 25 Lines |