korganizer

kojournaleditor.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 1997, 1998 Preston Brown <pbrown@kde.org>
00005     Copyright (c) 2000-2003 Cornelius Schumacher <schumacher@kde.org>
00006     Copyright (c) 2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00007 
00008     This program is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with this program; if not, write to the Free Software
00020     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021 
00022     As a special exception, permission is given to link this program
00023     with any edition of Qt, and distribute the resulting executable,
00024     without including the source code for Qt in the source distribution.
00025 */
00026 
00027 #include "kojournaleditor.h"
00028 
00029 #include "koeditorgeneraljournal.h"
00030 #include "koeditordetails.h"
00031 #include "kodialogmanager.h"
00032 #include "koprefs.h"
00033 
00034 #include <libkcal/journal.h>
00035 #include <libkcal/calendarlocal.h>
00036 #include <korganizer/baseview.h>
00037 
00038 #include <kmessagebox.h>
00039 #include <klocale.h>
00040 #include <kdebug.h>
00041 
00042 #include <qlayout.h>
00043 
00044 using namespace KCal;
00045 
00046 KOJournalEditor::KOJournalEditor( Calendar *calendar, QWidget *parent ) :
00047   KOIncidenceEditor( i18n("Edit Journal Entry"), calendar, parent )
00048 {
00049   mJournal = 0;
00050 }
00051 
00052 KOJournalEditor::~KOJournalEditor()
00053 {
00054   emit dialogClose( mJournal );
00055 }
00056 
00057 void KOJournalEditor::init()
00058 {
00059   setupGeneral();
00060   setupAttendeesTab();
00061 }
00062 
00063 void KOJournalEditor::reload()
00064 {
00065   kdDebug(5851) << "reloading Journal" << endl;
00066   if ( mJournal ) {
00067     readJournal( mJournal, QDate() );
00068   }
00069 }
00070 
00071 void KOJournalEditor::setupGeneral()
00072 {
00073   mGeneral = new KOEditorGeneralJournal(this);
00074 
00075   if (KOPrefs::instance()->mCompactDialogs) {
00076     QFrame *topFrame = addPage(i18n("General"));
00077 
00078     QBoxLayout *topLayout = new QVBoxLayout( topFrame );
00079     topLayout->setMargin( marginHint() );
00080     topLayout->setSpacing( spacingHint() );
00081 
00082     mGeneral->initTitle( topFrame, topLayout );
00083     mGeneral->initDate( topFrame, topLayout );
00084     mGeneral->initDescription( topFrame, topLayout );
00085   } else {
00086     QFrame *topFrame = addPage(i18n("&General"));
00087 
00088     QBoxLayout *topLayout = new QVBoxLayout(topFrame);
00089     topLayout->setSpacing(spacingHint());
00090 
00091     mGeneral->initTitle( topFrame, topLayout );
00092     mGeneral->initDate( topFrame, topLayout );
00093     mGeneral->initDescription( topFrame, topLayout );
00094   }
00095 
00096   mGeneral->finishSetup();
00097 }
00098 
00099 void KOJournalEditor::editIncidence( Incidence *incidence, const QDate &date, Calendar * )
00100 {
00101   Journal *journal=dynamic_cast<Journal*>(incidence);
00102   if (journal)
00103   {
00104     init();
00105 
00106     mJournal = journal;
00107     readJournal(mJournal, date);
00108   }
00109 }
00110 
00111 
00112 void KOJournalEditor::newJournal()
00113 {
00114   init();
00115   mJournal = 0;
00116   loadDefaults();
00117 }
00118 
00119 void KOJournalEditor::setTexts( const QString &summary, const QString &description )
00120 {
00121   if ( description.isEmpty() && summary.contains("\n") ) {
00122     mGeneral->setDescription( summary );
00123     int pos = summary.find( "\n" );
00124     mGeneral->setSummary( summary.left( pos ) );
00125   } else {
00126     mGeneral->setSummary( summary );
00127     mGeneral->setDescription( description );
00128   }
00129 }
00130 
00131 
00132 
00133 void KOJournalEditor::loadDefaults()
00134 {
00135   setDate( QDate::currentDate() );
00136 }
00137 
00138 bool KOJournalEditor::processInput()
00139 {
00140   if ( !validateInput() ) return false;
00141 
00142   if ( mJournal ) {
00143     Journal *oldJournal = mJournal->clone();
00144     writeJournal( mJournal );
00145     mChanger->changeIncidence( oldJournal, mJournal, KOGlobals::NOTHING_MODIFIED, this );
00146     delete oldJournal;
00147   } else {
00148     mJournal = new Journal;
00149     mJournal->setOrganizer( Person( KOPrefs::instance()->fullName(),
00150                             KOPrefs::instance()->email() ) );
00151 
00152     writeJournal( mJournal );
00153 
00154     if ( !mChanger->addIncidence( mJournal, this ) ) {
00155       KODialogManager::errorSaveIncidence( this, mJournal );
00156       delete mJournal;
00157       mJournal = 0;
00158       return false;
00159     }
00160   }
00161 
00162   return true;
00163 }
00164 
00165 void KOJournalEditor::deleteJournal()
00166 {
00167   kdDebug(5850) << "Delete journal" << endl;
00168 
00169   if ( mJournal )
00170     emit deleteIncidenceSignal( mJournal );
00171   emit dialogClose( mJournal );
00172   reject();
00173 }
00174 
00175 void KOJournalEditor::setDate( const QDate &date )
00176 {
00177   mGeneral->setDefaults( date );
00178   mDetails->setDefaults();
00179 }
00180 
00181 void KOJournalEditor::readJournal( Journal *journal, const QDate &date )
00182 {
00183   kdDebug(5851)<<"read Journal"<<endl;
00184   mGeneral->readJournal( journal, date );
00185   mDetails->readEvent( journal );
00186 }
00187 
00188 void KOJournalEditor::writeJournal( Journal *journal )
00189 {
00190   mGeneral->writeJournal( journal );
00191   mDetails->writeEvent( journal );
00192 }
00193 
00194 bool KOJournalEditor::validateInput()
00195 {
00196   return mGeneral->validateInput() && mDetails->validateInput();
00197 }
00198 
00199 int KOJournalEditor::msgItemDelete()
00200 {
00201   return KMessageBox::warningContinueCancel( this,
00202       i18n("This journal entry will be permanently deleted."),
00203       i18n("KOrganizer Confirmation"), KGuiItem( i18n("Delete"), "editdelete" ));
00204 }
00205 
00206 void KOJournalEditor::modified()
00207 {
00208   // Play dump, just reload the Journal. This dialog has become so complicated
00209   // that there is no point in trying to be smart here...
00210   reload();
00211 }
00212 
00213 void KOJournalEditor::loadTemplate( /*const*/ CalendarLocal& cal)
00214 {
00215   Journal::List journals = cal.journals();
00216   if ( journals.count() == 0 ) {
00217     KMessageBox::error( this,
00218         i18n("Template does not contain a valid journal.") );
00219   } else {
00220     readJournal( journals.first(), QDate() );
00221   }
00222 }
00223 
00224 void KOJournalEditor::slotSaveTemplate( const QString &templateName )
00225 {
00226   Journal *journal = new Journal;
00227   writeJournal( journal );
00228   saveAsTemplate( journal, templateName );
00229 }
00230 
00231 QStringList& KOJournalEditor::templates() const
00232 {
00233   return KOPrefs::instance()->mJournalTemplates;
00234 }
00235 #include "kojournaleditor.moc"