1 #include "managesievescriptsdialog.h"
2 #include "managesievescriptsdialog_p.h"
4 #include "sieveconfig.h"
5 #include "accountmanager.h"
6 #include "imapaccountbase.h"
10 #include <tdelocale.h>
11 #include <kiconloader.h>
13 #include <tdeapplication.h>
14 #include <kinputdialog.h>
15 #include <tdeglobalsettings.h>
16 #include <tdemessagebox.h>
19 #include <tqlistview.h>
20 #include <tqtextedit.h>
21 #include <tqpopupmenu.h>
25 inline TQCheckListItem * qcli_cast( TQListViewItem * lvi ) {
26 return lvi && lvi->rtti() == 1 ?
static_cast<TQCheckListItem*
>( lvi ) : 0 ;
28 inline const TQCheckListItem * qcli_cast(
const TQListViewItem * lvi ) {
29 return lvi && lvi->rtti() == 1 ?
static_cast<const TQCheckListItem*
>( lvi ) : 0 ;
32 KMail::ManageSieveScriptsDialog::ManageSieveScriptsDialog( TQWidget * parent,
const char * name )
33 : KDialogBase( Plain, i18n(
"Manage Sieve Scripts" ), Close, Close,
34 parent, name, false ),
36 mContextMenuItem( 0 ),
39 setWFlags( WGroupLeader|WDestructiveClose );
40 KWin::setIcons( winId(), kapp->icon(), kapp->miniIcon() );
42 TQVBoxLayout * vlay =
new TQVBoxLayout( plainPage(), 0, 0 );
44 mListView =
new TQListView( plainPage() );
45 mListView->addColumn( i18n(
"Available Scripts" ) );
46 mListView->setResizeMode( TQListView::LastColumn );
47 mListView->setRootIsDecorated(
true );
48 mListView->setSelectionMode( TQListView::Single );
49 connect( mListView, TQ_SIGNAL(contextMenuRequested(TQListViewItem*,
const TQPoint&,
int)),
50 this, TQ_SLOT(slotContextMenuRequested(TQListViewItem*,
const TQPoint&)) );
51 connect( mListView, TQ_SIGNAL(doubleClicked(TQListViewItem*,
const TQPoint&,
int)),
52 this, TQ_SLOT(slotDoubleClicked(TQListViewItem*)) );
53 connect( mListView, TQ_SIGNAL(selectionChanged(TQListViewItem*)),
54 this, TQ_SLOT(slotSelectionChanged(TQListViewItem*)) );
55 vlay->addWidget( mListView );
57 resize( 2 * sizeHint().width(), sizeHint().height() );
62 KMail::ManageSieveScriptsDialog::~ManageSieveScriptsDialog() {
66 void KMail::ManageSieveScriptsDialog::killAllJobs() {
67 for ( TQMap<SieveJob*,TQCheckListItem*>::const_iterator it = mJobs.constBegin(), end = mJobs.constEnd() ; it != end ; ++it )
72 static KURL findUrlForAccount(
const KMail::ImapAccountBase * a ) {
74 const KMail::SieveConfig sieve = a->sieveConfig();
75 if ( !sieve.managesieveSupported() )
77 if ( sieve.reuseConfig() ) {
80 u.setProtocol(
"sieve" );
81 u.setHost( a->host() );
82 u.setUser( a->login() );
83 u.setPass( a->passwd() );
84 u.setPort( sieve.port() );
86 u.addQueryItem(
"x-mech", a->auth() ==
"*" ?
"PLAIN" : a->auth() );
87 if ( !a->useSSL() && !a->useTLS() )
88 u.addQueryItem(
"x-allow-unencrypted",
"true" );
91 KURL u = sieve.alternateURL();
92 if ( u.protocol().lower() ==
"sieve" && !a->useSSL() && !a->useTLS() && u.queryItem(
"x-allow-unencrypted").isEmpty() )
93 u.addQueryItem(
"x-allow-unencrypted",
"true" );
98 void KMail::ManageSieveScriptsDialog::slotRefresh() {
105 TQCheckListItem * last = 0;
106 for ( KMAccount * a = am->
first() ; a ; a = am->
next() ) {
107 last =
new TQCheckListItem( mListView, last, a->name(), TQCheckListItem::Controller );
108 last->setPixmap( 0, SmallIcon(
"server" ) );
109 if ( ImapAccountBase * iab =
dynamic_cast<ImapAccountBase*
>( a ) ) {
110 const KURL u = ::findUrlForAccount( iab );
113 SieveJob * job = SieveJob::list( u );
114 connect( job, TQ_SIGNAL(item(KMail::SieveJob*,
const TQString&,
bool)),
115 this, TQ_SLOT(slotItem(KMail::SieveJob*,
const TQString&,
bool)) );
116 connect( job, TQ_SIGNAL(result(KMail::SieveJob*,
bool,
const TQString&,
bool)),
117 this, TQ_SLOT(slotResult(KMail::SieveJob*,
bool,
const TQString&,
bool)) );
118 mJobs.insert( job, last );
119 mUrls.insert( last, u );
121 TQListViewItem * item =
new TQListViewItem( last, i18n(
"No Sieve URL configured" ) );
122 item->setEnabled(
false );
123 last->setOpen(
true );
128 void KMail::ManageSieveScriptsDialog::slotResult( KMail::SieveJob * job,
bool success,
const TQString &,
bool ) {
129 TQCheckListItem * parent = mJobs[job];
135 parent->setOpen(
true );
140 TQListViewItem * item =
new TQListViewItem( parent, i18n(
"Failed to fetch the list of scripts" ) );
141 item->setEnabled(
false );
144 void KMail::ManageSieveScriptsDialog::slotItem( KMail::SieveJob * job,
const TQString & filename,
bool isActive ) {
145 TQCheckListItem * parent = mJobs[job];
148 TQCheckListItem * item =
new TQCheckListItem( parent, filename, TQCheckListItem::RadioButton );
151 mSelectedItems[parent] = item;
155 void KMail::ManageSieveScriptsDialog::slotContextMenuRequested( TQListViewItem * i,
const TQPoint & p ) {
156 TQCheckListItem * item = qcli_cast( i );
159 if ( !item->depth() && !mUrls.count( item ) )
162 mContextMenuItem = item;
163 if ( item->depth() ) {
165 menu.insertItem( i18n(
"Delete Script" ),
this, TQ_SLOT(slotDeleteScript()) );
166 menu.insertItem( i18n(
"Edit Script..." ),
this, TQ_SLOT(slotEditScript()) );
167 menu.insertItem( i18n(
"Deactivate Script" ),
this, TQ_SLOT(slotDeactivateScript()) );
170 menu.insertItem( i18n(
"New Script..." ),
this, TQ_SLOT(slotNewScript()) );
173 mContextMenuItem = 0;
177 void KMail::ManageSieveScriptsDialog::slotDeactivateScript() {
178 if ( !mContextMenuItem )
181 TQCheckListItem * parent = qcli_cast( mContextMenuItem->parent() );
184 if ( mContextMenuItem->isOn()) {
185 mSelectedItems[parent] = mContextMenuItem;
186 changeActiveScript( parent,
false );
190 void KMail::ManageSieveScriptsDialog::slotSelectionChanged( TQListViewItem * i ) {
191 TQCheckListItem * item = qcli_cast( i );
194 TQCheckListItem * parent = qcli_cast( item->parent() );
197 if ( item->isOn() && mSelectedItems[parent] != item ) {
198 mSelectedItems[parent] = item;
199 changeActiveScript( parent,
true );
203 void KMail::ManageSieveScriptsDialog::changeActiveScript( TQCheckListItem * item ,
bool activate) {
206 if ( !mUrls.count( item ) )
208 if ( !mSelectedItems.count( item ) )
210 KURL u = mUrls[item];
213 TQCheckListItem * selected = mSelectedItems[item];
216 u.setFileName( selected->text( 0 ) );
219 job = SieveJob::activate( u );
221 job = SieveJob::desactivate( u );
222 connect( job, TQ_SIGNAL(result(KMail::SieveJob*,
bool,
const TQString&,
bool)),
223 this, TQ_SLOT(slotRefresh()) );
226 void KMail::ManageSieveScriptsDialog::slotDoubleClicked( TQListViewItem * i ) {
227 TQCheckListItem * item = qcli_cast( i );
230 if ( !item->depth() )
232 mContextMenuItem = item;
234 mContextMenuItem = 0;
237 void KMail::ManageSieveScriptsDialog::slotDeleteScript() {
238 if ( !mContextMenuItem )
240 if ( !mContextMenuItem->depth() )
243 TQCheckListItem * parent = qcli_cast( mContextMenuItem->parent() );
247 if ( !mUrls.count( parent ) )
250 KURL u = mUrls[parent];
254 u.setFileName( mContextMenuItem->text( 0 ) );
256 if ( KMessageBox::warningContinueCancel(
this, i18n(
"Really delete script \"%1\" from the server?" ).arg( u.fileName() ),
257 i18n(
"Delete Sieve Script Confirmation" ),
259 != KMessageBox::Continue )
261 SieveJob * job = SieveJob::del( u );
262 connect( job, TQ_SIGNAL(result(KMail::SieveJob*,
bool,
const TQString&,
bool)),
263 this, TQ_SLOT(slotRefresh()) );
266 void KMail::ManageSieveScriptsDialog::slotEditScript() {
267 if ( !mContextMenuItem )
269 if ( !mContextMenuItem->depth() )
271 TQCheckListItem * parent = qcli_cast( mContextMenuItem->parent() );
272 if ( !mUrls.count( parent ) )
274 KURL url = mUrls[parent];
277 url.setFileName( mContextMenuItem->text( 0 ) );
279 SieveJob * job = SieveJob::get( url );
280 connect( job, TQ_SIGNAL(result(KMail::SieveJob*,
bool,
const TQString&,
bool)),
281 this, TQ_SLOT(slotGetResult(KMail::SieveJob*,
bool,
const TQString&,
bool)) );
284 void KMail::ManageSieveScriptsDialog::slotNewScript() {
285 if ( !mContextMenuItem )
287 if ( mContextMenuItem->depth() )
288 mContextMenuItem = qcli_cast( mContextMenuItem->parent() );
289 if ( !mContextMenuItem )
292 if ( !mUrls.count( mContextMenuItem ) )
295 KURL u = mUrls[mContextMenuItem];
300 const TQString name = KInputDialog::getText( i18n(
"New Sieve Script" ),
301 i18n(
"Please enter a name for the new Sieve script:" ),
302 i18n(
"unnamed" ), &ok,
this );
303 if ( !ok || name.isEmpty() )
306 u.setFileName( name );
308 (void)
new TQCheckListItem( mContextMenuItem, name, TQCheckListItem::RadioButton );
311 slotGetResult( 0,
true, TQString(),
false );
314 KMail::SieveEditor::SieveEditor( TQWidget * parent,
const char * name )
315 : KDialogBase( Plain, i18n(
"Edit Sieve Script" ),
Ok|Cancel,
Ok, parent, name )
317 TQVBoxLayout * vlay =
new TQVBoxLayout( plainPage(), 0, spacingHint() );
318 mTextEdit =
new TQTextEdit( plainPage() );
319 vlay->addWidget( mTextEdit );
320 mTextEdit->setFocus();
321 mTextEdit->setTextFormat( TQTextEdit::PlainText );
322 mTextEdit->setWordWrap( TQTextEdit::NoWrap );
323 mTextEdit->setFont( TDEGlobalSettings::fixedFont() );
324 connect( mTextEdit, TQ_SIGNAL( textChanged () ), TQ_SLOT( slotTextChanged() ) );
325 resize( 3 * sizeHint() );
328 KMail::SieveEditor::~SieveEditor() {}
331 void KMail::SieveEditor::slotTextChanged()
333 enableButtonOK( !script().isEmpty() );
336 void KMail::ManageSieveScriptsDialog::slotGetResult( KMail::SieveJob *,
bool success,
const TQString & script,
bool isActive ) {
343 mSieveEditor =
new SieveEditor(
this );
344 mSieveEditor->setScript( script );
345 connect( mSieveEditor, TQ_SIGNAL(okClicked()),
this, TQ_SLOT(slotSieveEditorOkClicked()) );
346 connect( mSieveEditor, TQ_SIGNAL(cancelClicked()),
this, TQ_SLOT(slotSieveEditorCancelClicked()) );
347 mSieveEditor->show();
348 mWasActive = isActive;
351 void KMail::ManageSieveScriptsDialog::slotSieveEditorOkClicked() {
354 SieveJob * job = SieveJob::put( mCurrentURL,mSieveEditor->script(), mWasActive, mWasActive );
355 connect( job, TQ_SIGNAL(result(KMail::SieveJob*,
bool,
const TQString&,
bool)),
356 this, TQ_SLOT(slotPutResult(KMail::SieveJob*,
bool)) );
359 void KMail::ManageSieveScriptsDialog::slotSieveEditorCancelClicked() {
360 mSieveEditor->deleteLater(); mSieveEditor = 0;
361 mCurrentURL = KURL();
365 void KMail::ManageSieveScriptsDialog::slotPutResult( KMail::SieveJob *,
bool success ) {
367 KMessageBox::information(
this, i18n(
"The Sieve script was successfully uploaded." ),
368 i18n(
"Sieve Script Upload" ) );
369 mSieveEditor->deleteLater(); mSieveEditor = 0;
370 mCurrentURL = KURL();
372 mSieveEditor->show();
376 #include "managesievescriptsdialog.moc"
377 #include "managesievescriptsdialog_p.moc"
The account manager is responsible for creating accounts of various types via the factory method crea...
const KMAccount * first() const
First account of the list.
const KMAccount * next() const
Next account of the list.
@ Ok
The user rights/ACL have been fetched from the server sucessfully.