knotes_part.cpp
1/*
2 This file is part of the KDE project
3 Copyright (C) 2002-2003 Daniel Molkentin <molkentin@kde.org>
4 Copyright (C) 2004-2006 Michael Brade <brade@kde.org>
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22#include <tqpopupmenu.h>
23#include <tqclipboard.h>
24
25#include <tdeapplication.h>
26#include <kdebug.h>
27#include <tdeaction.h>
28#include <tdemessagebox.h>
29
30#include <libtdepim/infoextension.h>
31#include <libtdepim/sidebarextension.h>
32
33#include "knotes/knoteprinter.h"
34#include "knotes/resourcemanager.h"
35
36#include "knotes_part.h"
37#include "knotes_part_p.h"
38#include "knotetip.h"
39
40
41KNotesPart::KNotesPart( TQObject *parent, const char *name )
42 : DCOPObject( "KNotesIface" ), KParts::ReadOnlyPart( parent, name ),
43 mNotesView( new KNotesIconView() ),
44 mNoteTip( new KNoteTip( mNotesView ) ),
45 mNoteEditDlg( 0 ),
46 mManager( new KNotesResourceManager() )
47{
48 mNoteList.setAutoDelete( true );
49
50 setInstance( new TDEInstance( "knotes" ) );
51
52 // create the actions
53 new TDEAction( i18n( "&New" ), "knotes", CTRL+Key_N, this, TQ_SLOT( newNote() ),
54 actionCollection(), "file_new" );
55 new TDEAction( i18n( "Rename..." ), "text", this, TQ_SLOT( renameNote() ),
56 actionCollection(), "edit_rename" );
57 new TDEAction( i18n( "Delete" ), "edit-delete", Key_Delete, this, TQ_SLOT( killSelectedNotes() ),
58 actionCollection(), "edit_delete" );
59 new TDEAction( i18n( "Print Selected Notes..." ), "print", CTRL+Key_P, this, TQ_SLOT( printSelectedNotes() ),
60 actionCollection(), "print_note" );
61
62 // TODO icons: s/editdelete/knotes_delete/ or the other way round in knotes
63
64 // set the view up
65 mNotesView->setSelectionMode( TQIconView::Extended );
66 mNotesView->setItemsMovable( false );
67 mNotesView->setResizeMode( TQIconView::Adjust );
68 mNotesView->setAutoArrange( true );
69 mNotesView->setSorting( true );
70
71 connect( mNotesView, TQ_SIGNAL( executed( TQIconViewItem* ) ),
72 this, TQ_SLOT( editNote( TQIconViewItem* ) ) );
73 connect( mNotesView, TQ_SIGNAL( returnPressed( TQIconViewItem* ) ),
74 this, TQ_SLOT( editNote( TQIconViewItem* ) ) );
75 connect( mNotesView, TQ_SIGNAL( itemRenamed( TQIconViewItem* ) ),
76 this, TQ_SLOT( renamedNote( TQIconViewItem* ) ) );
77 connect( mNotesView, TQ_SIGNAL( contextMenuRequested( TQIconViewItem*, const TQPoint& ) ),
78 this, TQ_SLOT( popupRMB( TQIconViewItem*, const TQPoint& ) ) );
79 connect( mNotesView, TQ_SIGNAL( onItem( TQIconViewItem* ) ),
80 this, TQ_SLOT( slotOnItem( TQIconViewItem* ) ) );
81 connect( mNotesView, TQ_SIGNAL( onViewport() ),
82 this, TQ_SLOT( slotOnViewport() ) );
83 connect( mNotesView, TQ_SIGNAL( currentChanged( TQIconViewItem* ) ),
84 this, TQ_SLOT( slotOnCurrentChanged( TQIconViewItem* ) ) );
85
86 slotOnCurrentChanged( 0 );
87
88 new KParts::SideBarExtension( mNotesView, this, "NotesSideBarExtension" );
89
90 setWidget( mNotesView );
91 setXMLFile( "knotes_part.rc" );
92
93 // connect the resource manager
94 connect( mManager, TQ_SIGNAL( sigRegisteredNote( KCal::Journal* ) ),
95 this, TQ_SLOT( createNote( KCal::Journal* ) ) );
96 connect( mManager, TQ_SIGNAL( sigDeregisteredNote( KCal::Journal* ) ),
97 this, TQ_SLOT( killNote( KCal::Journal* ) ) );
98
99 // read the notes
100 mManager->load();
101}
102
103KNotesPart::~KNotesPart()
104{
105 delete mNoteTip;
106 mNoteTip = 0;
107
108 delete mManager;
109 mManager = 0;
110}
111
112void KNotesPart::printSelectedNotes()
113{
114 TQValueList<KCal::Journal*> journals;
115
116 for ( TQIconViewItem *it = mNotesView->firstItem(); it; it = it->nextItem() ) {
117 if ( it->isSelected() ) {
118 journals.append( static_cast<KNotesIconViewItem *>( it )->journal() );
119 }
120 }
121
122 if ( journals.isEmpty() ) {
123 KMessageBox::information( mNotesView, i18n("To print notes, first select the notes to print from the list."), i18n("Print Notes") );
124 return;
125 }
126
127 KNotePrinter printer;
128 printer.printNotes(journals );
129
130#if 0
131 TQString content;
132 if ( m_editor->textFormat() == PlainText )
133 content = TQStyleSheet::convertFromPlainText( m_editor->text() );
134 else
135 content = m_editor->text();
136
137 KNotePrinter printer;
138 printer.setMimeSourceFactory( m_editor->mimeSourceFactory() );
139 //printer.setFont( m_config->font() );
140 //printer.setContext( m_editor->context() );
141 //printer.setStyleSheet( m_editor->styleSheet() );
142 printer.setColorGroup( colorGroup() );
143 printer.printNote( , content );
144#endif
145}
146
147bool KNotesPart::openFile()
148{
149 return false;
150}
151
152
153// public KNotes DCOP interface implementation
154
155TQString KNotesPart::newNote( const TQString& name, const TQString& text )
156{
157 // create the new note
158 KCal::Journal *journal = new KCal::Journal();
159
160 // new notes have the current date/time as title if none was given
161 if ( !name.isEmpty() )
162 journal->setSummary( name );
163 else
164 journal->setSummary( TDEGlobal::locale()->formatDateTime( TQDateTime::currentDateTime() ) );
165
166 // the body of the note
167 journal->setDescription( text );
168
169
170
171 // Edit the new note if text is empty
172 if ( text.isNull() )
173 {
174 if ( !mNoteEditDlg )
175 mNoteEditDlg = new KNoteEditDlg( widget() );
176
177 mNoteEditDlg->setTitle( journal->summary() );
178 mNoteEditDlg->setText( journal->description() );
179
180 if ( mNoteEditDlg->exec() == TQDialog::Accepted )
181 {
182 journal->setSummary( mNoteEditDlg->title() );
183 journal->setDescription( mNoteEditDlg->text() );
184 }
185 else
186 {
187 delete journal;
188 return "";
189 }
190 }
191
192 mManager->addNewNote( journal );
193 mManager->save();
194
195 KNotesIconViewItem *note = mNoteList[ journal->uid() ];
196 mNotesView->ensureItemVisible( note );
197 mNotesView->setCurrentItem( note );
198
199 return journal->uid();
200}
201
202TQString KNotesPart::newNoteFromClipboard( const TQString& name )
203{
204 const TQString& text = TDEApplication::clipboard()->text();
205 return newNote( name, text );
206}
207
208void KNotesPart::killNote( const TQString& id )
209{
210 killNote( id, false );
211}
212
213void KNotesPart::killNote( const TQString& id, bool force )
214{
215 KNotesIconViewItem *note = mNoteList[ id ];
216
217 if ( note &&
218 ( (!force && KMessageBox::warningContinueCancelList( mNotesView,
219 i18n( "Do you really want to delete this note?" ),
220 mNoteList[ id ]->text(), i18n( "Confirm Delete" ),
221 KStdGuiItem::del() ) == KMessageBox::Continue)
222 || force )
223 )
224 {
225 mManager->deleteNote( mNoteList[id]->journal() );
226 mManager->save();
227 }
228}
229
230TQString KNotesPart::name( const TQString& id ) const
231{
232 KNotesIconViewItem *note = mNoteList[ id ];
233 if ( note )
234 return note->text();
235 else
236 return TQString();
237}
238
239TQString KNotesPart::text( const TQString& id ) const
240{
241 KNotesIconViewItem *note = mNoteList[id];
242 if ( note )
243 return note->journal()->description();
244 else
245 return TQString();
246}
247
248void KNotesPart::setName( const TQString& id, const TQString& newName )
249{
250 KNotesIconViewItem *note = mNoteList[ id ];
251 if ( note ) {
252 note->setText( newName );
253 mManager->save();
254 }
255}
256
257void KNotesPart::setText( const TQString& id, const TQString& newText )
258{
259 KNotesIconViewItem *note = mNoteList[ id ];
260 if ( note ) {
261 note->journal()->setDescription( newText );
262 mManager->save();
263 }
264}
265
266TQMap<TQString, TQString> KNotesPart::notes() const
267{
268 TQMap<TQString, TQString> notes;
269 TQDictIterator<KNotesIconViewItem> it( mNoteList );
270
271 for ( ; it.current(); ++it )
272 notes.insert( (*it)->journal()->uid(), (*it)->journal()->summary() );
273
274 return notes;
275}
276
277TQDateTime KNotesPart::getLastModified( const TQString& id ) const
278{
279 KNotesIconViewItem *note = mNoteList[ id ];
280 TQDateTime dt;
281 if ( note )
282 dt = note->journal()->lastModified();
283 return dt;
284}
285
286// private stuff
287
288void KNotesPart::killSelectedNotes()
289{
290 TQPtrList<KNotesIconViewItem> items;
291 TQStringList notes;
292
293 KNotesIconViewItem *knivi;
294 for ( TQIconViewItem *it = mNotesView->firstItem(); it; it = it->nextItem() ) {
295 if ( it->isSelected() ) {
296 knivi = static_cast<KNotesIconViewItem *>( it );
297 items.append( knivi );
298 notes.append( knivi->text() );
299 }
300 }
301
302 if ( items.isEmpty() )
303 return;
304
305 int ret = KMessageBox::warningContinueCancelList( mNotesView,
306 i18n( "Do you really want to delete this note?",
307 "Do you really want to delete these %n notes?", items.count() ),
308 notes, i18n( "Confirm Delete" ),
309 KStdGuiItem::del() );
310
311 if ( ret == KMessageBox::Continue ) {
312 TQPtrListIterator<KNotesIconViewItem> kniviIt( items );
313 while ( (knivi = *kniviIt) ) {
314 ++kniviIt;
315 mManager->deleteNote( knivi->journal() );
316 }
317
318 mManager->save();
319 }
320}
321
322void KNotesPart::popupRMB( TQIconViewItem *item, const TQPoint& pos )
323{
324 TQPopupMenu *contextMenu = NULL;
325
326 if ( item )
327 contextMenu = static_cast<TQPopupMenu *>( factory()->container( "note_context", this ) );
328 else
329 contextMenu = static_cast<TQPopupMenu *>( factory()->container( "notepart_context", this ) );
330
331 if ( !contextMenu )
332 return;
333
334 contextMenu->popup( pos );
335}
336
337void KNotesPart::slotOnItem( TQIconViewItem *i )
338{
339 // TODO: disable (i.e. setNote( TQString() )) when mouse button pressed
340
341 KNotesIconViewItem *item = static_cast<KNotesIconViewItem *>( i );
342 mNoteTip->setNote( item );
343}
344
345void KNotesPart::slotOnViewport()
346{
347 mNoteTip->setNote( 0 );
348}
349
350// TODO: also with takeItem, clear(),
351
352// create and kill the icon view item corresponding to the note, edit the note
353
354void KNotesPart::createNote( KCal::Journal *journal )
355{
356 // make sure all fields are existent, initialize them with default values
357 TQString property = journal->customProperty( "KNotes", "BgColor" );
358 if ( property.isNull() )
359 journal->setCustomProperty( "KNotes", "BgColor", "#ffff00" );
360
361 property = journal->customProperty( "KNotes", "FgColor" );
362 if ( property.isNull() )
363 journal->setCustomProperty( "KNotes", "FgColor", "#000000" );
364
365 property = journal->customProperty( "KNotes", "RichText" );
366 if ( property.isNull() )
367 journal->setCustomProperty( "KNotes", "RichText", "true" );
368
369 mNoteList.insert( journal->uid(), new KNotesIconViewItem( mNotesView, journal ) );
370}
371
372void KNotesPart::killNote( KCal::Journal *journal )
373{
374 mNoteList.remove( journal->uid() );
375}
376
377void KNotesPart::editNote( TQIconViewItem *item )
378{
379 if ( !mNoteEditDlg )
380 mNoteEditDlg = new KNoteEditDlg( widget() );
381
382 KCal::Journal *journal = static_cast<KNotesIconViewItem *>( item )->journal();
383
384 mNoteEditDlg->setRichText( journal->customProperty( "KNotes", "RichText" ) == "true" );
385 mNoteEditDlg->setTitle( journal->summary() );
386 mNoteEditDlg->setText( journal->description() );
387
388 if ( mNoteEditDlg->exec() == TQDialog::Accepted ) {
389 item->setText( mNoteEditDlg->title() );
390 journal->setDescription( mNoteEditDlg->text() );
391 mManager->save();
392 }
393}
394
395void KNotesPart::renameNote()
396{
397 mOldName = mNotesView->currentItem()->text();
398 mNotesView->currentItem()->rename();
399}
400
401void KNotesPart::renamedNote( TQIconViewItem* )
402{
403 if ( mOldName != mNotesView->currentItem()->text() )
404 mManager->save();
405}
406
407void KNotesPart::slotOnCurrentChanged( TQIconViewItem* )
408{
409 TDEAction *renameAction = actionCollection()->action( "edit_rename" );
410 TDEAction *deleteAction = actionCollection()->action( "edit_delete" );
411
412 if ( !mNotesView->currentItem() ) {
413 renameAction->setEnabled( false );
414 deleteAction->setEnabled( false );
415 } else {
416 renameAction->setEnabled( true );
417 deleteAction->setEnabled( true );
418 }
419}
420
421#include "knotes_part.moc"
422#include "knotes_part_p.moc"
423
void setCustomProperty(const TQCString &app, const TQCString &key, const TQString &value)
TQString customProperty(const TQCString &app, const TQCString &key) const
TQString uid() const
void setSummary(const TQString &summary)
TQString description() const
void setDescription(const TQString &description)
TQString summary() const