knotes_part.cpp
00001 /* 00002 This file is part of the KDE project 00003 Copyright (C) 2002-2003 Daniel Molkentin <molkentin@kde.org> 00004 Copyright (C) 2004-2006 Michael Brade <brade@kde.org> 00005 00006 This program is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (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 GNU 00014 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; see the file COPYING. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 00022 #include <tqpopupmenu.h> 00023 #include <tqclipboard.h> 00024 00025 #include <kapplication.h> 00026 #include <kdebug.h> 00027 #include <kaction.h> 00028 #include <kmessagebox.h> 00029 00030 #include <libkdepim/infoextension.h> 00031 #include <libkdepim/sidebarextension.h> 00032 00033 #include "knotes/knoteprinter.h" 00034 #include "knotes/resourcemanager.h" 00035 00036 #include "knotes_part.h" 00037 #include "knotes_part_p.h" 00038 #include "knotetip.h" 00039 00040 00041 KNotesPart::KNotesPart( TQObject *parent, const char *name ) 00042 : DCOPObject( "KNotesIface" ), KParts::ReadOnlyPart( parent, name ), 00043 mNotesView( new KNotesIconView() ), 00044 mNoteTip( new KNoteTip( mNotesView ) ), 00045 mNoteEditDlg( 0 ), 00046 mManager( new KNotesResourceManager() ) 00047 { 00048 mNoteList.setAutoDelete( true ); 00049 00050 setInstance( new KInstance( "knotes" ) ); 00051 00052 // create the actions 00053 new KAction( i18n( "&New" ), "knotes", CTRL+Key_N, this, TQT_SLOT( newNote() ), 00054 actionCollection(), "file_new" ); 00055 new KAction( i18n( "Rename..." ), "text", this, TQT_SLOT( renameNote() ), 00056 actionCollection(), "edit_rename" ); 00057 new KAction( i18n( "Delete" ), "editdelete", Key_Delete, this, TQT_SLOT( killSelectedNotes() ), 00058 actionCollection(), "edit_delete" ); 00059 new KAction( i18n( "Print Selected Notes..." ), "print", CTRL+Key_P, this, TQT_SLOT( printSelectedNotes() ), 00060 actionCollection(), "print_note" ); 00061 00062 // TODO icons: s/editdelete/knotes_delete/ or the other way round in knotes 00063 00064 // set the view up 00065 mNotesView->setSelectionMode( TQIconView::Extended ); 00066 mNotesView->setItemsMovable( false ); 00067 mNotesView->setResizeMode( TQIconView::Adjust ); 00068 mNotesView->setAutoArrange( true ); 00069 mNotesView->setSorting( true ); 00070 00071 connect( mNotesView, TQT_SIGNAL( executed( TQIconViewItem* ) ), 00072 this, TQT_SLOT( editNote( TQIconViewItem* ) ) ); 00073 connect( mNotesView, TQT_SIGNAL( returnPressed( TQIconViewItem* ) ), 00074 this, TQT_SLOT( editNote( TQIconViewItem* ) ) ); 00075 connect( mNotesView, TQT_SIGNAL( itemRenamed( TQIconViewItem* ) ), 00076 this, TQT_SLOT( renamedNote( TQIconViewItem* ) ) ); 00077 connect( mNotesView, TQT_SIGNAL( contextMenuRequested( TQIconViewItem*, const TQPoint& ) ), 00078 this, TQT_SLOT( popupRMB( TQIconViewItem*, const TQPoint& ) ) ); 00079 connect( mNotesView, TQT_SIGNAL( onItem( TQIconViewItem* ) ), 00080 this, TQT_SLOT( slotOnItem( TQIconViewItem* ) ) ); 00081 connect( mNotesView, TQT_SIGNAL( onViewport() ), 00082 this, TQT_SLOT( slotOnViewport() ) ); 00083 connect( mNotesView, TQT_SIGNAL( currentChanged( TQIconViewItem* ) ), 00084 this, TQT_SLOT( slotOnCurrentChanged( TQIconViewItem* ) ) ); 00085 00086 slotOnCurrentChanged( 0 ); 00087 00088 new KParts::SideBarExtension( mNotesView, this, "NotesSideBarExtension" ); 00089 00090 setWidget( mNotesView ); 00091 setXMLFile( "knotes_part.rc" ); 00092 00093 // connect the resource manager 00094 connect( mManager, TQT_SIGNAL( sigRegisteredNote( KCal::Journal* ) ), 00095 this, TQT_SLOT( createNote( KCal::Journal* ) ) ); 00096 connect( mManager, TQT_SIGNAL( sigDeregisteredNote( KCal::Journal* ) ), 00097 this, TQT_SLOT( killNote( KCal::Journal* ) ) ); 00098 00099 // read the notes 00100 mManager->load(); 00101 } 00102 00103 KNotesPart::~KNotesPart() 00104 { 00105 delete mNoteTip; 00106 mNoteTip = 0; 00107 00108 delete mManager; 00109 mManager = 0; 00110 } 00111 00112 void KNotesPart::printSelectedNotes() 00113 { 00114 TQValueList<KCal::Journal*> journals; 00115 00116 for ( TQIconViewItem *it = mNotesView->firstItem(); it; it = it->nextItem() ) { 00117 if ( it->isSelected() ) { 00118 journals.append( static_cast<KNotesIconViewItem *>( it )->journal() ); 00119 } 00120 } 00121 00122 if ( journals.isEmpty() ) { 00123 KMessageBox::information( mNotesView, i18n("To print notes, first select the notes to print from the list."), i18n("Print Notes") ); 00124 return; 00125 } 00126 00127 KNotePrinter printer; 00128 printer.printNotes(journals ); 00129 00130 #if 0 00131 TQString content; 00132 if ( m_editor->textFormat() == PlainText ) 00133 content = TQStyleSheet::convertFromPlainText( m_editor->text() ); 00134 else 00135 content = m_editor->text(); 00136 00137 KNotePrinter printer; 00138 printer.setMimeSourceFactory( m_editor->mimeSourceFactory() ); 00139 //printer.setFont( m_config->font() ); 00140 //printer.setContext( m_editor->context() ); 00141 //printer.setStyleSheet( m_editor->styleSheet() ); 00142 printer.setColorGroup( colorGroup() ); 00143 printer.printNote( , content ); 00144 #endif 00145 } 00146 00147 bool KNotesPart::openFile() 00148 { 00149 return false; 00150 } 00151 00152 00153 // public KNotes DCOP interface implementation 00154 00155 TQString KNotesPart::newNote( const TQString& name, const TQString& text ) 00156 { 00157 // create the new note 00158 KCal::Journal *journal = new KCal::Journal(); 00159 00160 // new notes have the current date/time as title if none was given 00161 if ( !name.isEmpty() ) 00162 journal->setSummary( name ); 00163 else 00164 journal->setSummary( KGlobal::locale()->formatDateTime( TQDateTime::currentDateTime() ) ); 00165 00166 // the body of the note 00167 journal->setDescription( text ); 00168 00169 00170 00171 // Edit the new note if text is empty 00172 if ( text.isNull() ) 00173 { 00174 if ( !mNoteEditDlg ) 00175 mNoteEditDlg = new KNoteEditDlg( widget() ); 00176 00177 mNoteEditDlg->setTitle( journal->summary() ); 00178 mNoteEditDlg->setText( journal->description() ); 00179 00180 if ( mNoteEditDlg->exec() == TQDialog::Accepted ) 00181 { 00182 journal->setSummary( mNoteEditDlg->title() ); 00183 journal->setDescription( mNoteEditDlg->text() ); 00184 } 00185 else 00186 { 00187 delete journal; 00188 return ""; 00189 } 00190 } 00191 00192 mManager->addNewNote( journal ); 00193 mManager->save(); 00194 00195 KNotesIconViewItem *note = mNoteList[ journal->uid() ]; 00196 mNotesView->ensureItemVisible( note ); 00197 mNotesView->setCurrentItem( note ); 00198 00199 return journal->uid(); 00200 } 00201 00202 TQString KNotesPart::newNoteFromClipboard( const TQString& name ) 00203 { 00204 const TQString& text = KApplication::clipboard()->text(); 00205 return newNote( name, text ); 00206 } 00207 00208 void KNotesPart::killNote( const TQString& id ) 00209 { 00210 killNote( id, false ); 00211 } 00212 00213 void KNotesPart::killNote( const TQString& id, bool force ) 00214 { 00215 KNotesIconViewItem *note = mNoteList[ id ]; 00216 00217 if ( note && 00218 ( (!force && KMessageBox::warningContinueCancelList( mNotesView, 00219 i18n( "Do you really want to delete this note?" ), 00220 mNoteList[ id ]->text(), i18n( "Confirm Delete" ), 00221 KStdGuiItem::del() ) == KMessageBox::Continue) 00222 || force ) 00223 ) 00224 { 00225 mManager->deleteNote( mNoteList[id]->journal() ); 00226 mManager->save(); 00227 } 00228 } 00229 00230 TQString KNotesPart::name( const TQString& id ) const 00231 { 00232 KNotesIconViewItem *note = mNoteList[ id ]; 00233 if ( note ) 00234 return note->text(); 00235 else 00236 return TQString(); 00237 } 00238 00239 TQString KNotesPart::text( const TQString& id ) const 00240 { 00241 KNotesIconViewItem *note = mNoteList[id]; 00242 if ( note ) 00243 return note->journal()->description(); 00244 else 00245 return TQString(); 00246 } 00247 00248 void KNotesPart::setName( const TQString& id, const TQString& newName ) 00249 { 00250 KNotesIconViewItem *note = mNoteList[ id ]; 00251 if ( note ) { 00252 note->setText( newName ); 00253 mManager->save(); 00254 } 00255 } 00256 00257 void KNotesPart::setText( const TQString& id, const TQString& newText ) 00258 { 00259 KNotesIconViewItem *note = mNoteList[ id ]; 00260 if ( note ) { 00261 note->journal()->setDescription( newText ); 00262 mManager->save(); 00263 } 00264 } 00265 00266 TQMap<TQString, TQString> KNotesPart::notes() const 00267 { 00268 TQMap<TQString, TQString> notes; 00269 TQDictIterator<KNotesIconViewItem> it( mNoteList ); 00270 00271 for ( ; it.current(); ++it ) 00272 notes.insert( (*it)->journal()->uid(), (*it)->journal()->summary() ); 00273 00274 return notes; 00275 } 00276 00277 00278 // private stuff 00279 00280 void KNotesPart::killSelectedNotes() 00281 { 00282 TQPtrList<KNotesIconViewItem> items; 00283 TQStringList notes; 00284 00285 KNotesIconViewItem *knivi; 00286 for ( TQIconViewItem *it = mNotesView->firstItem(); it; it = it->nextItem() ) { 00287 if ( it->isSelected() ) { 00288 knivi = static_cast<KNotesIconViewItem *>( it ); 00289 items.append( knivi ); 00290 notes.append( knivi->text() ); 00291 } 00292 } 00293 00294 if ( items.isEmpty() ) 00295 return; 00296 00297 int ret = KMessageBox::warningContinueCancelList( mNotesView, 00298 i18n( "Do you really want to delete this note?", 00299 "Do you really want to delete these %n notes?", items.count() ), 00300 notes, i18n( "Confirm Delete" ), 00301 KStdGuiItem::del() ); 00302 00303 if ( ret == KMessageBox::Continue ) { 00304 TQPtrListIterator<KNotesIconViewItem> kniviIt( items ); 00305 while ( (knivi = *kniviIt) ) { 00306 ++kniviIt; 00307 mManager->deleteNote( knivi->journal() ); 00308 } 00309 00310 mManager->save(); 00311 } 00312 } 00313 00314 void KNotesPart::popupRMB( TQIconViewItem *item, const TQPoint& pos ) 00315 { 00316 TQPopupMenu *contextMenu = NULL; 00317 00318 if ( item ) 00319 contextMenu = static_cast<TQPopupMenu *>( factory()->container( "note_context", this ) ); 00320 else 00321 contextMenu = static_cast<TQPopupMenu *>( factory()->container( "notepart_context", this ) ); 00322 00323 if ( !contextMenu ) 00324 return; 00325 00326 contextMenu->popup( pos ); 00327 } 00328 00329 void KNotesPart::slotOnItem( TQIconViewItem *i ) 00330 { 00331 // TODO: disable (i.e. setNote( TQString() )) when mouse button pressed 00332 00333 KNotesIconViewItem *item = static_cast<KNotesIconViewItem *>( i ); 00334 mNoteTip->setNote( item ); 00335 } 00336 00337 void KNotesPart::slotOnViewport() 00338 { 00339 mNoteTip->setNote( 0 ); 00340 } 00341 00342 // TODO: also with takeItem, clear(), 00343 00344 // create and kill the icon view item corresponding to the note, edit the note 00345 00346 void KNotesPart::createNote( KCal::Journal *journal ) 00347 { 00348 // make sure all fields are existent, initialize them with default values 00349 TQString property = journal->customProperty( "KNotes", "BgColor" ); 00350 if ( property.isNull() ) 00351 journal->setCustomProperty( "KNotes", "BgColor", "#ffff00" ); 00352 00353 property = journal->customProperty( "KNotes", "FgColor" ); 00354 if ( property.isNull() ) 00355 journal->setCustomProperty( "KNotes", "FgColor", "#000000" ); 00356 00357 property = journal->customProperty( "KNotes", "RichText" ); 00358 if ( property.isNull() ) 00359 journal->setCustomProperty( "KNotes", "RichText", "true" ); 00360 00361 mNoteList.insert( journal->uid(), new KNotesIconViewItem( mNotesView, journal ) ); 00362 } 00363 00364 void KNotesPart::killNote( KCal::Journal *journal ) 00365 { 00366 mNoteList.remove( journal->uid() ); 00367 } 00368 00369 void KNotesPart::editNote( TQIconViewItem *item ) 00370 { 00371 if ( !mNoteEditDlg ) 00372 mNoteEditDlg = new KNoteEditDlg( widget() ); 00373 00374 KCal::Journal *journal = static_cast<KNotesIconViewItem *>( item )->journal(); 00375 00376 mNoteEditDlg->setRichText( journal->customProperty( "KNotes", "RichText" ) == "true" ); 00377 mNoteEditDlg->setTitle( journal->summary() ); 00378 mNoteEditDlg->setText( journal->description() ); 00379 00380 if ( mNoteEditDlg->exec() == TQDialog::Accepted ) { 00381 item->setText( mNoteEditDlg->title() ); 00382 journal->setDescription( mNoteEditDlg->text() ); 00383 mManager->save(); 00384 } 00385 } 00386 00387 void KNotesPart::renameNote() 00388 { 00389 mOldName = mNotesView->currentItem()->text(); 00390 mNotesView->currentItem()->rename(); 00391 } 00392 00393 void KNotesPart::renamedNote( TQIconViewItem* ) 00394 { 00395 if ( mOldName != mNotesView->currentItem()->text() ) 00396 mManager->save(); 00397 } 00398 00399 void KNotesPart::slotOnCurrentChanged( TQIconViewItem* ) 00400 { 00401 KAction *renameAction = actionCollection()->action( "edit_rename" ); 00402 KAction *deleteAction = actionCollection()->action( "edit_delete" ); 00403 00404 if ( !mNotesView->currentItem() ) { 00405 renameAction->setEnabled( false ); 00406 deleteAction->setEnabled( false ); 00407 } else { 00408 renameAction->setEnabled( true ); 00409 deleteAction->setEnabled( true ); 00410 } 00411 } 00412 00413 #include "knotes_part.moc" 00414 #include "knotes_part_p.moc" 00415