recurrenceactions.cpp
00001 /* 00002 This file is part of the kcal library. 00003 00004 Copyright (C) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.net 00005 Author: Kevin Krammer, krake@kdab.com 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to 00019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. 00021 */ 00022 00023 #include "recurrenceactions.h" 00024 00025 #include "ui_recurrenceactionsscopewidget.h" 00026 00027 #include <KDialog> 00028 #include <KLocale> 00029 #include <KMessageBox> 00030 00031 #include <QPointer> 00032 00033 #include <boost/shared_ptr.hpp> 00034 00035 using namespace KCalUtils; 00036 using namespace KCalUtils::RecurrenceActions; 00037 using namespace KCalCore; 00038 00039 class ScopeWidget : public QWidget 00040 { 00041 public: 00042 ScopeWidget( int availableChoices, const KDateTime &dateTime, QWidget *parent ) 00043 : QWidget( parent ), mAvailableChoices( availableChoices ) 00044 { 00045 mUi.setupUi( this ); 00046 00047 if ( ( mAvailableChoices & PastOccurrences ) == 0 ) { 00048 mUi.checkBoxPast->hide(); 00049 } else { 00050 mUi.checkBoxPast->setText( i18nc( "@option:check calendar items before a certain date", 00051 "Items before %1", 00052 KGlobal::locale()->formatDateTime( dateTime ) ) ); 00053 } 00054 if ( ( mAvailableChoices & SelectedOccurrence ) == 0 ) { 00055 mUi.checkBoxSelected->hide(); 00056 } else { 00057 mUi.checkBoxSelected->setText( i18nc( "@option:check currently selected calendar item", 00058 "Selected item" ) ); 00059 } 00060 if ( ( mAvailableChoices & FutureOccurrences ) == 0 ) { 00061 mUi.checkBoxFuture->hide(); 00062 } else { 00063 mUi.checkBoxFuture->setText( i18nc( "@option:check calendar items after a certain date", 00064 "Items after %1", 00065 KGlobal::locale()->formatDateTime( dateTime ) ) ); 00066 } 00067 } 00068 00069 void setMessage( const QString &message ); 00070 void setIcon( const QIcon &icon ); 00071 00072 void setCheckedChoices( int choices ); 00073 int checkedChoices() const; 00074 00075 private: 00076 const int mAvailableChoices; 00077 Ui_RecurrenceActionsScopeWidget mUi; 00078 }; 00079 00080 void ScopeWidget::setMessage( const QString &message ) 00081 { 00082 mUi.messageLabel->setText( message ); 00083 } 00084 00085 void ScopeWidget::setIcon( const QIcon &icon ) 00086 { 00087 QStyleOption option; 00088 option.initFrom( this ); 00089 mUi.iconLabel->setPixmap( 00090 icon.pixmap( style()->pixelMetric( QStyle::PM_MessageBoxIconSize, &option, this ) ) ); 00091 } 00092 00093 void ScopeWidget::setCheckedChoices( int choices ) 00094 { 00095 // mask with available ones 00096 choices &= mAvailableChoices; 00097 00098 mUi.checkBoxPast->setChecked( ( choices & PastOccurrences ) != 0 ); 00099 mUi.checkBoxSelected->setChecked( ( choices & SelectedOccurrence ) != 0 ); 00100 mUi.checkBoxFuture->setChecked( ( choices & FutureOccurrences ) != 0 ); 00101 } 00102 00103 int ScopeWidget::checkedChoices() const 00104 { 00105 int result = NoOccurrence; 00106 00107 if ( mUi.checkBoxPast->isChecked() ) { 00108 result |= PastOccurrences; 00109 } 00110 if ( mUi.checkBoxSelected->isChecked() ) { 00111 result |= SelectedOccurrence; 00112 } 00113 if ( mUi.checkBoxFuture->isChecked() ) { 00114 result |= FutureOccurrences; 00115 } 00116 00117 return result; 00118 } 00119 00120 int RecurrenceActions::availableOccurrences( const Incidence::Ptr &incidence, 00121 const KDateTime &selectedOccurrence ) 00122 { 00123 int result = NoOccurrence; 00124 00125 if ( incidence->recurrence()->recursOn( selectedOccurrence.date(), 00126 selectedOccurrence.timeSpec() ) ) { 00127 result |= SelectedOccurrence; 00128 } 00129 00130 if ( incidence->recurrence()->getPreviousDateTime( selectedOccurrence ).isValid() ) { 00131 result |= PastOccurrences; 00132 } 00133 00134 if ( incidence->recurrence()->getNextDateTime( selectedOccurrence ).isValid() ) { 00135 result |= FutureOccurrences; 00136 } 00137 00138 return result; 00139 } 00140 00141 int RecurrenceActions::questionMultipleChoice( const KDateTime &selectedOccurrence, 00142 const QString &message, const QString &caption, 00143 const KGuiItem &action, int availableChoices, 00144 int preselectedChoices, QWidget *parent ) 00145 { 00146 QPointer<KDialog> dialog = new KDialog( parent ); 00147 dialog->setCaption( caption ); 00148 dialog->setButtons( KDialog::Ok | KDialog::Cancel ); 00149 dialog->setDefaultButton( KDialog::Ok ); 00150 dialog->setButtonGuiItem( KDialog::Ok, action ); 00151 00152 ScopeWidget *widget = new ScopeWidget( availableChoices, selectedOccurrence, dialog ); 00153 dialog->setMainWidget( widget ); 00154 00155 widget->setMessage( message ); 00156 widget->setIcon( widget->style()->standardIcon( QStyle::SP_MessageBoxQuestion ) ); 00157 widget->setCheckedChoices( preselectedChoices ); 00158 00159 const int result = dialog->exec(); 00160 if ( dialog ) { 00161 dialog->deleteLater(); 00162 } 00163 00164 if ( result == QDialog::Rejected ) { 00165 return NoOccurrence; 00166 } 00167 00168 return widget->checkedChoices(); 00169 } 00170 00171 int RecurrenceActions::questionSelectedAllCancel( const QString &message, const QString &caption, 00172 const KGuiItem &actionSelected, 00173 const KGuiItem &actionAll, QWidget *parent ) 00174 { 00175 KDialog *dialog = new KDialog( parent ); 00176 dialog->setCaption( caption ); 00177 dialog->setButtons( KDialog::Yes | KDialog::Ok | KDialog::Cancel ); 00178 dialog->setObjectName( "RecurrenceActions::questionSelectedAllCancel" ); 00179 dialog->setDefaultButton( KDialog::Yes ); 00180 dialog->setButtonGuiItem( KDialog::Yes, actionSelected ); 00181 dialog->setButtonGuiItem( KDialog::Ok, actionAll ); 00182 00183 bool checkboxResult = false; 00184 int result = KMessageBox::createKMessageBox( 00185 dialog, 00186 QMessageBox::Question, 00187 message, 00188 QStringList(), 00189 QString(), 00190 &checkboxResult, 00191 KMessageBox::Notify ); 00192 00193 switch (result) { 00194 case KDialog::Yes: 00195 return SelectedOccurrence; 00196 case QDialog::Accepted: 00197 // See kdialog.h, 'Ok' doesn't return KDialog:Ok 00198 return AllOccurrences; 00199 default: 00200 return NoOccurrence; 00201 } 00202 00203 return NoOccurrence; 00204 } 00205 00206 int RecurrenceActions::questionSelectedFutureAllCancel( const QString &message, 00207 const QString &caption, 00208 const KGuiItem &actionSelected, 00209 const KGuiItem &actionFuture, 00210 const KGuiItem &actionAll, 00211 QWidget *parent ) 00212 { 00213 KDialog *dialog = new KDialog( parent ); 00214 dialog->setCaption( caption ); 00215 dialog->setButtons( KDialog::Yes | KDialog::No | KDialog::Ok | KDialog::Cancel ); 00216 dialog->setObjectName( "RecurrenceActions::questionSelectedFutureAllCancel" ); 00217 dialog->setDefaultButton( KDialog::Yes ); 00218 dialog->setButtonGuiItem( KDialog::Yes, actionSelected ); 00219 dialog->setButtonGuiItem( KDialog::No, actionFuture ); 00220 dialog->setButtonGuiItem( KDialog::Ok, actionAll ); 00221 00222 bool checkboxResult = false; 00223 int result = KMessageBox::createKMessageBox( 00224 dialog, 00225 QMessageBox::Question, 00226 message, 00227 QStringList(), 00228 QString(), 00229 &checkboxResult, 00230 KMessageBox::Notify ); 00231 00232 switch (result) { 00233 case KDialog::Yes: 00234 return SelectedOccurrence; 00235 case KDialog::No: 00236 return FutureOccurrences; 00237 case QDialog::Accepted: 00238 return AllOccurrences; 00239 default: 00240 return NoOccurrence; 00241 } 00242 00243 return NoOccurrence; 00244 } 00245 00246 // kate: space-indent on; indent-width 2; replace-tabs on;