korganizer

koeditorgeneral.cpp

00001 /*
00002     This file is part of KOrganizer.
00003     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00004     Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 
00026 #include <qwidget.h>
00027 #include <qtooltip.h>
00028 #include <qlayout.h>
00029 #include <qvbox.h>
00030 #include <qhbox.h>
00031 #include <qbuttongroup.h>
00032 #include <qvgroupbox.h>
00033 #include <qwidgetstack.h>
00034 #include <qdatetime.h>
00035 #include <qlineedit.h>
00036 #include <qlabel.h>
00037 #include <qcheckbox.h>
00038 #include <qpushbutton.h>
00039 #include <qcombobox.h>
00040 #include <qspinbox.h>
00041 #include <qwhatsthis.h>
00042 
00043 #include <kglobal.h>
00044 #include <kdialog.h>
00045 #include <kdebug.h>
00046 #include <klocale.h>
00047 #include <kiconloader.h>
00048 #include <kmessagebox.h>
00049 #include <kfiledialog.h>
00050 #include <ksqueezedtextlabel.h>
00051 #include <kstandarddirs.h>
00052 #include <ktextedit.h>
00053 #include <krestrictedline.h>
00054 
00055 #include <libkcal/todo.h>
00056 #include <libkcal/event.h>
00057 
00058 #include <libkdepim/kdateedit.h>
00059 #include <libkdepim/categoryselectdialog.h>
00060 
00061 #include "koprefs.h"
00062 #include "koglobals.h"
00063 
00064 #include "koeditorgeneral.h"
00065 #include "koeditoralarms.h"
00066 #include "koeditorattachments.h"
00067 #include "koeditorgeneral.moc"
00068 #include "kohelper.h"
00069 
00070 KOEditorGeneral::KOEditorGeneral(QObject* parent, const char* name) :
00071   QObject( parent, name ), mAttachments(0)
00072 {
00073   mAlarmList.setAutoDelete( true );
00074 }
00075 
00076 KOEditorGeneral::~KOEditorGeneral()
00077 {
00078 }
00079 
00080 
00081 FocusLineEdit::FocusLineEdit( QWidget *parent )
00082   : QLineEdit( parent ), mSkipFirst( true )
00083 {
00084 }
00085 
00086 void FocusLineEdit::focusInEvent ( QFocusEvent *e )
00087 {
00088   if ( !mSkipFirst ) {
00089     emit focusReceivedSignal();
00090   } else {
00091     mSkipFirst = false;
00092   }
00093   QLineEdit::focusInEvent( e );
00094 }
00095 
00096 
00097 void KOEditorGeneral::initHeader( QWidget *parent,QBoxLayout *topLayout)
00098 {
00099   QGridLayout *headerLayout = new QGridLayout();
00100   headerLayout->setSpacing( topLayout->spacing() );
00101   topLayout->addLayout( headerLayout );
00102 
00103 #if 0
00104   mOwnerLabel = new QLabel(i18n("Owner:"),parent);
00105   headerLayout->addMultiCellWidget(mOwnerLabel,0,0,0,1);
00106 #endif
00107 
00108   QString whatsThis = i18n("Sets the Title of this event or to-do.");
00109   QLabel *summaryLabel = new QLabel( i18n("T&itle:"), parent );
00110   QWhatsThis::add( summaryLabel, whatsThis );
00111   QFont f = summaryLabel->font();
00112   f.setBold( true );
00113   summaryLabel->setFont(f);
00114   headerLayout->addWidget(summaryLabel,1,0);
00115 
00116   mSummaryEdit = new FocusLineEdit( parent );
00117   QWhatsThis::add( mSummaryEdit, whatsThis );
00118   connect( mSummaryEdit, SIGNAL( focusReceivedSignal() ),
00119            SIGNAL( focusReceivedSignal() ) );
00120   headerLayout->addWidget(mSummaryEdit,1,1);
00121   summaryLabel->setBuddy( mSummaryEdit );
00122 
00123   mAttendeeSummaryLabel = new QLabel( parent );
00124   updateAttendeeSummary( 0 );
00125   headerLayout->addWidget( mAttendeeSummaryLabel, 1, 2 );
00126 
00127   whatsThis = i18n("Sets where the event or to-do will take place.");
00128   QLabel *locationLabel = new QLabel( i18n("&Location:"), parent );
00129   QWhatsThis::add( locationLabel, whatsThis );
00130   headerLayout->addWidget(locationLabel,2,0);
00131 
00132   mLocationEdit = new QLineEdit( parent );
00133   QWhatsThis::add( mLocationEdit, whatsThis );
00134   headerLayout->addMultiCellWidget( mLocationEdit, 2, 2, 1, 2 );
00135   locationLabel->setBuddy( mLocationEdit );
00136 
00137   QBoxLayout *thirdLineLayout = new QHBoxLayout();
00138   headerLayout->addMultiCellLayout( thirdLineLayout, 3, 3, 0, 2 );
00139 
00140   mResourceLabel = new QLabel( parent );
00141   mResourceLabel->hide();
00142   thirdLineLayout->addWidget( mResourceLabel );
00143 
00144   whatsThis = i18n("Allows you to select the categories that this event or to-do belongs to.");
00145   QLabel *categoriesLabel = new QLabel( i18n("Categories:"), parent );
00146   QWhatsThis::add( categoriesLabel, whatsThis );
00147   thirdLineLayout->addWidget( categoriesLabel );
00148   mCategoriesLabel = new KSqueezedTextLabel( parent );
00149   QWhatsThis::add( mCategoriesLabel, whatsThis );
00150   mCategoriesLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);
00151   thirdLineLayout->addWidget( mCategoriesLabel );
00152 
00153   mCategoriesButton = new QPushButton( parent );
00154   mCategoriesButton->setText(i18n("&Select..."));
00155   QWhatsThis::add( mCategoriesButton, whatsThis );
00156   connect(mCategoriesButton,SIGNAL(clicked()),SLOT(selectCategories()));
00157   thirdLineLayout->addWidget( mCategoriesButton );
00158 }
00159 
00160 void KOEditorGeneral::initSecrecy(QWidget *parent, QBoxLayout *topLayout)
00161 {
00162   QBoxLayout *secrecyLayout = new QHBoxLayout( topLayout );
00163 
00164   QLabel *secrecyLabel = new QLabel(i18n("Acc&ess:"),parent);
00165   QString whatsThis = i18n("Sets whether the access to this event or to-do "
00166                "is restricted. Please note that KOrganizer "
00167                "currently does not use this setting, so the "
00168                "implementation of the restrictions will depend "
00169                "on the groupware server. This means that events "
00170                "or to-dos marked as private or confidential may "
00171                "be visible to others.");
00172   QWhatsThis::add( secrecyLabel, whatsThis );
00173   secrecyLayout->addWidget(secrecyLabel);
00174 
00175   mSecrecyCombo = new QComboBox(parent);
00176   QWhatsThis::add( mSecrecyCombo, whatsThis );
00177   mSecrecyCombo->insertStringList(Incidence::secrecyList());
00178   secrecyLayout->addWidget(mSecrecyCombo);
00179   secrecyLabel->setBuddy( mSecrecyCombo );
00180 }
00181 
00182 void KOEditorGeneral::initDescription(QWidget *parent,QBoxLayout *topLayout)
00183 {
00184   mDescriptionEdit = new KTextEdit(parent);
00185   QWhatsThis::add( mDescriptionEdit,
00186            i18n("Sets the description for this event or to-do. This "
00187             "will be displayed in a reminder if one is set, "
00188             "as well as in a tooltip when you hover over the "
00189             "event.") );
00190   mDescriptionEdit->append("");
00191   mDescriptionEdit->setReadOnly(false);
00192   mDescriptionEdit->setOverwriteMode(false);
00193   mDescriptionEdit->setWordWrap( KTextEdit::WidgetWidth );
00194   mDescriptionEdit->setTabChangesFocus( true );;
00195   topLayout->addWidget(mDescriptionEdit, 4);
00196 }
00197 
00198 void KOEditorGeneral::initAlarm(QWidget *parent,QBoxLayout *topLayout)
00199 {
00200   QBoxLayout *alarmLayout = new QHBoxLayout(topLayout);
00201 
00202   mAlarmBell = new QLabel(parent);
00203   mAlarmBell->setPixmap(KOGlobals::self()->smallIcon("bell"));
00204   alarmLayout->addWidget( mAlarmBell );
00205 
00206 
00207   mAlarmStack = new QWidgetStack( parent );
00208   alarmLayout->addWidget( mAlarmStack );
00209 
00210   mAlarmInfoLabel = new QLabel( i18n("No reminders configured"), mAlarmStack );
00211   mAlarmStack->addWidget( mAlarmInfoLabel, AdvancedAlarmLabel );
00212 
00213   QHBox *simpleAlarmBox = new QHBox( mAlarmStack );
00214   mAlarmStack->addWidget( simpleAlarmBox, SimpleAlarmPage );
00215 
00216   mAlarmButton = new QCheckBox(i18n("&Reminder:"), simpleAlarmBox );
00217   QWhatsThis::add( mAlarmButton,
00218        i18n("Activates a reminder for this event or to-do.") );
00219 
00220   QString whatsThis = i18n("Sets how long before the event occurs "
00221                            "the reminder will be triggered.");
00222   mAlarmTimeEdit = new QSpinBox( 0, 99999, 1, simpleAlarmBox, "alarmTimeEdit" );
00223   mAlarmTimeEdit->setValue( 0 );
00224   QWhatsThis::add( mAlarmTimeEdit, whatsThis );
00225 
00226   mAlarmIncrCombo = new QComboBox( false, simpleAlarmBox );
00227   QWhatsThis::add( mAlarmIncrCombo, whatsThis );
00228   mAlarmIncrCombo->insertItem( i18n("minute(s)") );
00229   mAlarmIncrCombo->insertItem( i18n("hour(s)") );
00230   mAlarmIncrCombo->insertItem( i18n("day(s)") );
00231 //  mAlarmIncrCombo->setMinimumHeight(20);
00232   connect(mAlarmButton, SIGNAL(toggled(bool)), mAlarmTimeEdit, SLOT(setEnabled(bool)));
00233   connect(mAlarmButton, SIGNAL(toggled(bool)), mAlarmIncrCombo, SLOT(setEnabled(bool)));
00234   mAlarmTimeEdit->setEnabled( false );
00235   mAlarmIncrCombo->setEnabled( false );
00236 
00237   mAlarmEditButton = new QPushButton( i18n("Advanced"), parent );
00238   mAlarmEditButton->setEnabled( false );
00239   alarmLayout->addWidget( mAlarmEditButton );
00240   connect( mAlarmButton, SIGNAL(toggled(bool)), mAlarmEditButton, SLOT(setEnabled( bool)));
00241   connect( mAlarmEditButton, SIGNAL( clicked() ),
00242       SLOT( editAlarms() ) );
00243 
00244 }
00245 
00246 void KOEditorGeneral::initAttachments(QWidget *parent,QBoxLayout *topLayout)
00247 {
00248   mAttachments = new KOEditorAttachments( KDialog::spacingHint(), parent );
00249   connect( mAttachments, SIGNAL( openURL( const KURL & ) ) ,
00250            this, SIGNAL( openURL( const KURL & ) ) );
00251   topLayout->addWidget( mAttachments, 1 );
00252 }
00253 
00254 void KOEditorGeneral::addAttachments( const QStringList &attachments,
00255                                       const QStringList &mimeTypes,
00256                                       bool inlineAttachments )
00257 {
00258   QStringList::ConstIterator it;
00259   uint i = 0;
00260   for ( it = attachments.begin(); it != attachments.end(); ++it, ++i ) {
00261     QString mimeType;
00262     if ( mimeTypes.count() > i )
00263       mimeType = mimeTypes[ i ];
00264     mAttachments->addAttachment( *it, mimeType, QString(), !inlineAttachments );
00265   }
00266 }
00267 
00268 void KOEditorGeneral::selectCategories()
00269 {
00270   KPIM::CategorySelectDialog *categoryDialog = new KPIM::CategorySelectDialog( KOPrefs::instance(), mCategoriesButton    );
00271   KOGlobals::fitDialogToScreen( categoryDialog );
00272   categoryDialog->setSelected( mCategories );
00273 
00274   connect(categoryDialog, SIGNAL(editCategories()), this, SIGNAL(openCategoryDialog()));
00275   connect(this, SIGNAL(updateCategoryConfig()), categoryDialog, SLOT(updateCategoryConfig()));
00276 
00277   if ( categoryDialog->exec() ) {
00278     setCategories( categoryDialog->selectedCategories() );
00279   }
00280   delete categoryDialog;
00281 }
00282 
00283 
00284 void KOEditorGeneral::editAlarms()
00285 {
00286   if ( mAlarmStack->id( mAlarmStack->visibleWidget() ) == SimpleAlarmPage ) {
00287     mAlarmList.clear();
00288     Alarm *al = alarmFromSimplePage();
00289     if ( al ) {
00290       mAlarmList.append( al );
00291     }
00292   }
00293 
00294   KOEditorAlarms *dlg = new KOEditorAlarms( &mAlarmList, mAlarmEditButton );
00295   if ( dlg->exec() != KDialogBase::Cancel ) {
00296     updateAlarmWidgets();
00297   }
00298 }
00299 
00300 
00301 void KOEditorGeneral::enableAlarm( bool enable )
00302 {
00303   mAlarmStack->setEnabled( enable );
00304   mAlarmEditButton->setEnabled( enable );
00305 }
00306 
00307 
00308 void KOEditorGeneral::toggleAlarm( bool on )
00309 {
00310     mAlarmButton->setChecked( on );
00311 }
00312 
00313 void KOEditorGeneral::setCategories( const QStringList &categories )
00314 {
00315   mCategoriesLabel->setText( categories.join(",") );
00316   mCategories = categories;
00317 }
00318 
00319 void KOEditorGeneral::setDefaults(bool /*allDay*/)
00320 {
00321 #if 0
00322   mOwnerLabel->setText(i18n("Owner: ") + KOPrefs::instance()->fullName());
00323 #endif
00324 
00325   mAlarmList.clear();
00326   updateDefaultAlarmTime();
00327   updateAlarmWidgets();
00328 
00329   mSecrecyCombo->setCurrentItem(Incidence::SecrecyPublic);
00330   mAttachments->setDefaults();
00331 }
00332 
00333 void KOEditorGeneral::updateDefaultAlarmTime()
00334 {
00335   int reminderTime = KOPrefs::instance()->mReminderTime;
00336   int index = KOPrefs::instance()->mReminderTimeUnits;
00337   if ( index < 0 || index > 2 ) {
00338     index = 0;
00339   }
00340   mAlarmTimeEdit->setValue( reminderTime );
00341   mAlarmIncrCombo->setCurrentItem( index );
00342 }
00343 
00344 void KOEditorGeneral::updateAlarmWidgets()
00345 {
00346   if ( mAlarmList.isEmpty() ) {
00347     mAlarmStack->raiseWidget( SimpleAlarmPage );
00348     mAlarmButton->setChecked( false );
00349     mAlarmEditButton->setEnabled( false );
00350   } else if ( mAlarmList.count() > 1 ) {
00351     mAlarmStack->raiseWidget( AdvancedAlarmLabel );
00352     mAlarmInfoLabel->setText( i18n("1 advanced reminder configured",
00353                                    "%n advanced reminders configured",
00354                                    mAlarmList.count() ) );
00355     mAlarmEditButton->setEnabled( true );
00356   } else {
00357     Alarm *alarm = mAlarmList.first();
00358     // Check if its the trivial type of alarm, which can be
00359     // configured with a simply spin box...
00360 
00361     if ( alarm->type() == Alarm::Display && alarm->text().isEmpty()
00362          && alarm->repeatCount() == 0 && !alarm->hasTime()
00363          && alarm->hasStartOffset() && alarm->startOffset().asSeconds() < 0 )  {
00364       mAlarmStack->raiseWidget( SimpleAlarmPage );
00365       mAlarmButton->setChecked( true );
00366       int offset = alarm->startOffset().asSeconds();
00367 
00368       offset = offset / -60; // make minutes
00369       int useoffset = offset;
00370       if (offset % (24*60) == 0) { // divides evenly into days?
00371         useoffset = offset / (24*60);
00372         mAlarmIncrCombo->setCurrentItem(2);
00373       } else if (offset % 60 == 0) { // divides evenly into hours?
00374         useoffset = offset / 60;
00375         mAlarmIncrCombo->setCurrentItem(1);
00376       }
00377       mAlarmTimeEdit->setValue( useoffset );
00378     } else {
00379       mAlarmStack->raiseWidget( AdvancedAlarmLabel );
00380       mAlarmInfoLabel->setText( i18n("1 advanced reminder configured") );
00381       mAlarmEditButton->setEnabled( true );
00382     }
00383   }
00384 }
00385 
00386 void KOEditorGeneral::readIncidence(Incidence *event, Calendar *calendar)
00387 {
00388   mSummaryEdit->setText(event->summary());
00389   mLocationEdit->setText(event->location());
00390 
00391   mDescriptionEdit->setText(event->description());
00392 
00393 #if 0
00394   // organizer information
00395   mOwnerLabel->setText(i18n("Owner: ") + event->organizer().fullName() );
00396 #endif
00397 
00398   mSecrecyCombo->setCurrentItem(event->secrecy());
00399 
00400   // set up alarm stuff
00401   mAlarmList.clear();
00402   Alarm::List::ConstIterator it;
00403   Alarm::List alarms = event->alarms();
00404   for( it = alarms.begin(); it != alarms.end(); ++it ) {
00405     Alarm *al = new Alarm( *(*it) );
00406     al->setParent( 0 );
00407     mAlarmList.append( al );
00408   }
00409   updateDefaultAlarmTime();
00410   updateAlarmWidgets();
00411 
00412   setCategories(event->categories());
00413 
00414   mAttachments->readIncidence( event );
00415 
00416   QString resLabel = KOHelper::resourceLabel( calendar, event );
00417   if ( !resLabel.isEmpty() ) {
00418     mResourceLabel->setText( i18n( "Calendar: %1" ).arg( resLabel ) );
00419     mResourceLabel->show();
00420   }
00421 }
00422 
00423 Alarm *KOEditorGeneral::alarmFromSimplePage() const
00424 {
00425   if ( mAlarmButton->isChecked() ) {
00426     Alarm *alarm = new Alarm( 0 );
00427     alarm->setDisplayAlarm("");
00428     alarm->setEnabled(true);
00429     QString tmpStr = mAlarmTimeEdit->text();
00430     int j = mAlarmTimeEdit->value() * -60;
00431     if (mAlarmIncrCombo->currentItem() == 1)
00432       j = j * 60;
00433     else if (mAlarmIncrCombo->currentItem() == 2)
00434       j = j * (60 * 24);
00435     alarm->setStartOffset( j );
00436     return alarm;
00437   } else {
00438     return 0;
00439   }
00440 }
00441 void KOEditorGeneral::writeIncidence(Incidence *event)
00442 {
00443 //  kdDebug(5850) << "KOEditorGeneral::writeEvent()" << endl;
00444 
00445   event->setSummary(mSummaryEdit->text());
00446   event->setLocation(mLocationEdit->text());
00447   event->setDescription(mDescriptionEdit->text());
00448   event->setCategories(mCategories);
00449   event->setSecrecy(mSecrecyCombo->currentItem());
00450 
00451   // alarm stuff
00452   event->clearAlarms();
00453   if ( mAlarmStack->id( mAlarmStack->visibleWidget() ) == SimpleAlarmPage ) {
00454     Alarm *al = alarmFromSimplePage();
00455     if ( al ) {
00456       al->setParent( event );
00457       event->addAlarm( al );
00458     }
00459   } else {
00460     // simply assign the list of alarms
00461     Alarm::List::ConstIterator it;
00462     for( it = mAlarmList.begin(); it != mAlarmList.end(); ++it ) {
00463       Alarm *al = new Alarm( *(*it) );
00464       al->setParent( event );
00465       al->setEnabled( true );
00466       event->addAlarm( al );
00467     }
00468   }
00469   mAttachments->writeIncidence( event );
00470 }
00471 
00472 void KOEditorGeneral::setSummary( const QString &text )
00473 {
00474   mSummaryEdit->setText( text );
00475 }
00476 
00477 void KOEditorGeneral::setDescription( const QString &text )
00478 {
00479   mDescriptionEdit->setText( text );
00480 }
00481 
00482 QObject *KOEditorGeneral::typeAheadReceiver() const
00483 {
00484   return mSummaryEdit;
00485 }
00486 
00487 void KOEditorGeneral::updateAttendeeSummary(int count)
00488 {
00489   if ( count <= 0 )
00490     mAttendeeSummaryLabel->setText( i18n("No attendees") );
00491   else
00492     mAttendeeSummaryLabel->setText( i18n( "One attendee", "%n attendees", count ) );
00493 }
KDE Home | KDE Accessibility Home | Description of Access Keys