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 TQt, and distribute the resulting executable,
00024     without including the source code for TQt 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 <tqlayout.h>
00043 
00044 using namespace KCal;
00045 
00046 KOJournalEditor::KOJournalEditor( Calendar *calendar, TQWidget *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, TQDate() );
00068   }
00069 }
00070 
00071 void KOJournalEditor::setupGeneral()
00072 {
00073   mGeneral = new KOEditorGeneralJournal(this);
00074 
00075   if (KOPrefs::instance()->mCompactDialogs) {
00076     TQFrame *topFrame = addPage(i18n("General"));
00077 
00078     TQBoxLayout *topLayout = new TQVBoxLayout( 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     TQFrame *topFrame = addPage(i18n("&General"));
00087 
00088     TQBoxLayout *topLayout = new TQVBoxLayout(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 TQDate &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 TQString &summary, const TQString &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( TQDate::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, mResource, mSubResource, this ) ) {
00155       delete mJournal;
00156       mJournal = 0;
00157       return false;
00158     }
00159   }
00160 
00161   return true;
00162 }
00163 
00164 void KOJournalEditor::deleteJournal()
00165 {
00166   kdDebug(5850) << "Delete journal" << endl;
00167 
00168   if ( mJournal )
00169     emit deleteIncidenceSignal( mJournal );
00170   emit dialogClose( mJournal );
00171   reject();
00172 }
00173 
00174 void KOJournalEditor::setDate( const TQDate &date )
00175 {
00176   mGeneral->setDefaults( date );
00177   mDetails->setDefaults();
00178 }
00179 
00180 void KOJournalEditor::readJournal( Journal *journal, const TQDate &date )
00181 {
00182   kdDebug(5851)<<"read Journal"<<endl;
00183   mGeneral->readJournal( journal, date );
00184   mDetails->readEvent( journal );
00185 }
00186 
00187 void KOJournalEditor::writeJournal( Journal *journal )
00188 {
00189   mGeneral->writeJournal( journal );
00190   mDetails->writeEvent( journal );
00191 }
00192 
00193 bool KOJournalEditor::validateInput()
00194 {
00195   return mGeneral->validateInput() && mDetails->validateInput();
00196 }
00197 
00198 int KOJournalEditor::msgItemDelete()
00199 {
00200   return KMessageBox::warningContinueCancel( this,
00201       i18n("This journal entry will be permanently deleted."),
00202       i18n("KOrganizer Confirmation"), KGuiItem( i18n("Delete"), "editdelete" ));
00203 }
00204 
00205 void KOJournalEditor::modified()
00206 {
00207   // Play dump, just reload the Journal. This dialog has become so complicated
00208   // that there is no point in trying to be smart here...
00209   reload();
00210 }
00211 
00212 void KOJournalEditor::loadTemplate( /*const*/ CalendarLocal& cal)
00213 {
00214   Journal::List journals = cal.journals();
00215   if ( journals.count() == 0 ) {
00216     KMessageBox::error( this,
00217         i18n("Template does not contain a valid journal.") );
00218   } else {
00219     readJournal( journals.first(), TQDate() );
00220   }
00221 }
00222 
00223 void KOJournalEditor::slotSaveTemplate( const TQString &templateName )
00224 {
00225   Journal *journal = new Journal;
00226   writeJournal( journal );
00227   saveAsTemplate( journal, templateName );
00228 }
00229 
00230 TQStringList& KOJournalEditor::templates() const
00231 {
00232   return KOPrefs::instance()->mJournalTemplates;
00233 }
00234 #include "kojournaleditor.moc"