korganizer

kojournaleditor.cpp
1 /*
2  This file is part of KOrganizer.
3 
4  Copyright (c) 1997, 1998 Preston Brown <pbrown@kde.org>
5  Copyright (c) 2000-2003 Cornelius Schumacher <schumacher@kde.org>
6  Copyright (c) 2004 Reinhold Kainhofer <reinhold@kainhofer.com>
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 
22  As a special exception, permission is given to link this program
23  with any edition of TQt, and distribute the resulting executable,
24  without including the source code for TQt in the source distribution.
25 */
26 
27 #include "kojournaleditor.h"
28 
29 #include "koeditorgeneraljournal.h"
30 #include "koeditordetails.h"
31 #include "kodialogmanager.h"
32 #include "koprefs.h"
33 
34 #include <libkcal/journal.h>
35 #include <libkcal/calendarlocal.h>
36 #include <korganizer/baseview.h>
37 
38 #include <tdemessagebox.h>
39 #include <tdelocale.h>
40 #include <kdebug.h>
41 
42 #include <tqlayout.h>
43 
44 using namespace KCal;
45 
46 KOJournalEditor::KOJournalEditor( Calendar *calendar, TQWidget *parent ) :
47  KOIncidenceEditor( i18n("Edit Journal Entry"), calendar, parent )
48 {
49  mJournal = 0;
50 }
51 
52 KOJournalEditor::~KOJournalEditor()
53 {
54  emit dialogClose( mJournal );
55 }
56 
58 {
59  setupGeneral();
60  setupAttendeesTab();
61 }
62 
63 void KOJournalEditor::reload()
64 {
65  kdDebug(5851) << "reloading Journal" << endl;
66  if ( mJournal ) {
67  readJournal( mJournal, TQDate() );
68  }
69 }
70 
71 void KOJournalEditor::setupGeneral()
72 {
73  mGeneral = new KOEditorGeneralJournal(this);
74 
75  if (KOPrefs::instance()->mCompactDialogs) {
76  TQFrame *topFrame = addPage(i18n("General"));
77 
78  TQBoxLayout *topLayout = new TQVBoxLayout( topFrame );
79  topLayout->setMargin( marginHint() );
80  topLayout->setSpacing( spacingHint() );
81 
82  mGeneral->initTitle( topFrame, topLayout );
83  mGeneral->initDate( topFrame, topLayout );
84  mGeneral->initDescription( topFrame, topLayout );
85  } else {
86  TQFrame *topFrame = addPage(i18n("&General"));
87 
88  TQBoxLayout *topLayout = new TQVBoxLayout(topFrame);
89  topLayout->setSpacing(spacingHint());
90 
91  mGeneral->initTitle( topFrame, topLayout );
92  mGeneral->initDate( topFrame, topLayout );
93  mGeneral->initDescription( topFrame, topLayout );
94  }
95 
96  mGeneral->finishSetup();
97 }
98 
99 void KOJournalEditor::editIncidence( Incidence *incidence, const TQDate &date, Calendar * )
100 {
101  Journal *journal=dynamic_cast<Journal*>(incidence);
102  if (journal)
103  {
104  init();
105 
106  mJournal = journal;
107  readJournal(mJournal, date);
108  }
109 }
110 
111 
113 {
114  init();
115  mJournal = 0;
116  loadDefaults();
117 }
118 
119 void KOJournalEditor::setTexts( const TQString &summary, const TQString &description )
120 {
121  if ( description.isEmpty() && summary.contains("\n") ) {
122  mGeneral->setDescription( summary );
123  int pos = summary.find( "\n" );
124  mGeneral->setSummary( summary.left( pos ) );
125  } else {
126  mGeneral->setSummary( summary );
127  mGeneral->setDescription( description );
128  }
129 }
130 
131 
132 
133 void KOJournalEditor::loadDefaults()
134 {
135  setDate( TQDate::currentDate() );
136 }
137 
139 {
140  if ( !validateInput() ) return false;
141 
142  if ( mJournal ) {
143  Journal *oldJournal = mJournal->clone();
144  writeJournal( mJournal );
145  mChanger->changeIncidence( oldJournal, mJournal, KOGlobals::NOTHING_MODIFIED, this );
146  delete oldJournal;
147  } else {
148  mJournal = new Journal;
149  mJournal->setOrganizer( Person( KOPrefs::instance()->fullName(),
150  KOPrefs::instance()->email() ) );
151 
152  writeJournal( mJournal );
153 
154  if ( !mChanger->addIncidence( mJournal, mResource, mSubResource, this ) ) {
155  delete mJournal;
156  mJournal = 0;
157  return false;
158  }
159  }
160 
161  return true;
162 }
163 
164 void KOJournalEditor::deleteJournal()
165 {
166  kdDebug(5850) << "Delete journal" << endl;
167 
168  if ( mJournal )
169  emit deleteIncidenceSignal( mJournal );
170  emit dialogClose( mJournal );
171  reject();
172 }
173 
174 void KOJournalEditor::setDate( const TQDate &date )
175 {
176  mGeneral->setDefaults( date );
177  mDetails->setDefaults();
178 }
179 
180 void KOJournalEditor::readJournal( Journal *journal, const TQDate &date )
181 {
182  kdDebug(5851)<<"read Journal"<<endl;
183  mGeneral->readJournal( journal, date );
184  mDetails->readEvent( journal );
185 }
186 
188 {
189  mGeneral->writeJournal( journal );
190  mDetails->writeEvent( journal );
191 }
192 
194 {
195  return mGeneral->validateInput() && mDetails->validateInput();
196 }
197 
198 int KOJournalEditor::msgItemDelete()
199 {
200  return KMessageBox::warningContinueCancel( this,
201  i18n("This journal entry will be permanently deleted."),
202  i18n("KOrganizer Confirmation"), KGuiItem( i18n("Delete"), "edit-delete" ));
203 }
204 
206 {
207  // Play dump, just reload the Journal. This dialog has become so complicated
208  // that there is no point in trying to be smart here...
209  reload();
210 }
211 
212 void KOJournalEditor::loadTemplate( /*const*/ CalendarLocal& cal)
213 {
214  Journal::List journals = cal.journals();
215  if ( journals.count() == 0 ) {
216  KMessageBox::error( this,
217  i18n("Template does not contain a valid journal.") );
218  } else {
219  readJournal( journals.first(), TQDate() );
220  }
221 }
222 
223 void KOJournalEditor::slotSaveTemplate( const TQString &templateName )
224 {
225  Journal *journal = new Journal;
226  writeJournal( journal );
227  saveAsTemplate( journal, templateName );
228 }
229 
230 TQStringList& KOJournalEditor::templates() const
231 {
232  return KOPrefs::instance()->mJournalTemplates;
233 }
234 #include "kojournaleditor.moc"
bool validateInput()
Check if the input is valid.
virtual Journal::List journals(JournalSortField sortField=JournalSortUnsorted, SortDirection sortDirection=SortDirectionAscending)
KOJournalEditor(Calendar *calendar, TQWidget *parent)
Constructs a new Journal editor.
void editIncidence(Incidence *, const TQDate &date, Calendar *)
Edit an existing Journal.
void newJournal()
Clear editor for new Journal.
void init()
Initialize editor.
void setTexts(const TQString &summary, const TQString &description=TQString())
Sets the given summary and description.
void setDate(const TQDate &date)
Set widgets to default values.
void setOrganizer(const Person &o)
Journal * clone()
void writeJournal(Journal *)
Write event settings to event object.
void readJournal(Journal *, const TQDate &date)
Read event object and setup widgets accordingly.
void modified()
This Journal has been modified externally.
bool processInput()
Process user input and create or update event.
This is the base class for the calendar component editors.