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, !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   // FIXME: Implement a KPrefsComboItem to solve this in a clean way.
00336 // FIXME: Use an int value for minutes instead of 5 hardcoded values
00337   int alarmTime;
00338   int a[] = { 1,5,10,15,30 };
00339   int index = KOPrefs::instance()->mAlarmTime;
00340   if (index < 0 || index > 4) {
00341     alarmTime = 0;
00342   } else {
00343     alarmTime = a[index];
00344   }
00345   mAlarmTimeEdit->setValue(alarmTime);
00346 }
00347 
00348 void KOEditorGeneral::updateAlarmWidgets()
00349 {
00350   if ( mAlarmList.isEmpty() ) {
00351     mAlarmStack->raiseWidget( SimpleAlarmPage );
00352     mAlarmButton->setChecked( false );
00353     mAlarmEditButton->setEnabled( false );
00354   } else if ( mAlarmList.count() > 1 ) {
00355     mAlarmStack->raiseWidget( AdvancedAlarmLabel );
00356     mAlarmInfoLabel->setText( i18n("1 advanced reminder configured",
00357                                    "%n advanced reminders configured",
00358                                    mAlarmList.count() ) );
00359     mAlarmEditButton->setEnabled( true );
00360   } else {
00361     Alarm *alarm = mAlarmList.first();
00362     // Check if its the trivial type of alarm, which can be
00363     // configured with a simply spin box...
00364 
00365     if ( alarm->type() == Alarm::Display && alarm->text().isEmpty()
00366          && alarm->repeatCount() == 0 && !alarm->hasTime()
00367          && alarm->hasStartOffset() && alarm->startOffset().asSeconds() < 0 )  {
00368       mAlarmStack->raiseWidget( SimpleAlarmPage );
00369       mAlarmButton->setChecked( true );
00370       int offset = alarm->startOffset().asSeconds();
00371 
00372       offset = offset / -60; // make minutes
00373       int useoffset = offset;
00374       if (offset % (24*60) == 0) { // divides evenly into days?
00375         useoffset = offset / (24*60);
00376         mAlarmIncrCombo->setCurrentItem(2);
00377       } else if (offset % 60 == 0) { // divides evenly into hours?
00378         useoffset = offset / 60;
00379         mAlarmIncrCombo->setCurrentItem(1);
00380       }
00381       mAlarmTimeEdit->setValue( useoffset );
00382     } else {
00383       mAlarmStack->raiseWidget( AdvancedAlarmLabel );
00384       mAlarmInfoLabel->setText( i18n("1 advanced reminder configured") );
00385       mAlarmEditButton->setEnabled( true );
00386     }
00387   }
00388 }
00389 
00390 void KOEditorGeneral::readIncidence(Incidence *event, Calendar *calendar)
00391 {
00392   mSummaryEdit->setText(event->summary());
00393   mLocationEdit->setText(event->location());
00394 
00395   mDescriptionEdit->setText(event->description());
00396 
00397 #if 0
00398   // organizer information
00399   mOwnerLabel->setText(i18n("Owner: ") + event->organizer().fullName() );
00400 #endif
00401 
00402   mSecrecyCombo->setCurrentItem(event->secrecy());
00403 
00404   // set up alarm stuff
00405   mAlarmList.clear();
00406   Alarm::List::ConstIterator it;
00407   Alarm::List alarms = event->alarms();
00408   for( it = alarms.begin(); it != alarms.end(); ++it ) {
00409     Alarm *al = new Alarm( *(*it) );
00410     al->setParent( 0 );
00411     mAlarmList.append( al );
00412   }
00413   updateDefaultAlarmTime();
00414   updateAlarmWidgets();
00415 
00416   setCategories(event->categories());
00417 
00418   mAttachments->readIncidence( event );
00419 
00420   QString resLabel = KOHelper::resourceLabel( calendar, event );
00421   if ( !resLabel.isEmpty() ) {
00422     mResourceLabel->setText( i18n( "Calendar: %1" ).arg( resLabel ) );
00423     mResourceLabel->show();
00424   }
00425 }
00426 
00427 Alarm *KOEditorGeneral::alarmFromSimplePage() const
00428 {
00429   if ( mAlarmButton->isChecked() ) {
00430     Alarm *alarm = new Alarm( 0 );
00431     alarm->setDisplayAlarm("");
00432     alarm->setEnabled(true);
00433     QString tmpStr = mAlarmTimeEdit->text();
00434     int j = mAlarmTimeEdit->value() * -60;
00435     if (mAlarmIncrCombo->currentItem() == 1)
00436       j = j * 60;
00437     else if (mAlarmIncrCombo->currentItem() == 2)
00438       j = j * (60 * 24);
00439     alarm->setStartOffset( j );
00440     return alarm;
00441   } else {
00442     return 0;
00443   }
00444 }
00445 void KOEditorGeneral::writeIncidence(Incidence *event)
00446 {
00447 //  kdDebug(5850) << "KOEditorGeneral::writeEvent()" << endl;
00448 
00449   event->setSummary(mSummaryEdit->text());
00450   event->setLocation(mLocationEdit->text());
00451   event->setDescription(mDescriptionEdit->text());
00452   event->setCategories(mCategories);
00453   event->setSecrecy(mSecrecyCombo->currentItem());
00454 
00455   // alarm stuff
00456   event->clearAlarms();
00457   if ( mAlarmStack->id( mAlarmStack->visibleWidget() ) == SimpleAlarmPage ) {
00458     Alarm *al = alarmFromSimplePage();
00459     if ( al ) {
00460       al->setParent( event );
00461       event->addAlarm( al );
00462     }
00463   } else {
00464     // simply assign the list of alarms
00465     Alarm::List::ConstIterator it;
00466     for( it = mAlarmList.begin(); it != mAlarmList.end(); ++it ) {
00467       Alarm *al = new Alarm( *(*it) );
00468       al->setParent( event );
00469       al->setEnabled( true );
00470       event->addAlarm( al );
00471     }
00472   }
00473   mAttachments->writeIncidence( event );
00474 }
00475 
00476 void KOEditorGeneral::setSummary( const QString &text )
00477 {
00478   mSummaryEdit->setText( text );
00479 }
00480 
00481 void KOEditorGeneral::setDescription( const QString &text )
00482 {
00483   mDescriptionEdit->setText( text );
00484 }
00485 
00486 QObject *KOEditorGeneral::typeAheadReceiver() const
00487 {
00488   return mSummaryEdit;
00489 }
00490 
00491 void KOEditorGeneral::updateAttendeeSummary(int count)
00492 {
00493   if ( count <= 0 )
00494     mAttendeeSummaryLabel->setText( i18n("No attendees") );
00495   else
00496     mAttendeeSummaryLabel->setText( i18n( "One attendee", "%n attendees", count ) );
00497 }
KDE Home | KDE Accessibility Home | Description of Access Keys