kontact

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 <qpopupmenu.h>
00023 #include <qclipboard.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/resourcemanager.h"
00034 
00035 #include "knotes_part.h"
00036 #include "knotes_part_p.h"
00037 #include "knotetip.h"
00038 
00039 
00040 KNotesPart::KNotesPart( QObject *parent, const char *name )
00041   : DCOPObject( "KNotesIface" ), KParts::ReadOnlyPart( parent, name ),
00042     mNotesView( new KIconView() ),
00043     mNoteTip( new KNoteTip( mNotesView ) ),
00044     mNoteEditDlg( 0 ),
00045     mManager( new KNotesResourceManager() )
00046 {
00047   mNoteList.setAutoDelete( true );
00048 
00049   setInstance( new KInstance( "knotes" ) );
00050 
00051   // create the actions
00052   new KAction( i18n( "&New" ), "knotes", CTRL+Key_N, this, SLOT( newNote() ),
00053                actionCollection(), "file_new" );
00054   new KAction( i18n( "Rename..." ), "text", this, SLOT( renameNote() ),
00055                actionCollection(), "edit_rename" );
00056   new KAction( i18n( "Delete" ), "editdelete", Key_Delete, this, SLOT( killSelectedNotes() ),
00057                actionCollection(), "edit_delete" );
00058 
00059   // TODO icons: s/editdelete/knotes_delete/ or the other way round in knotes
00060 
00061   // set the view up
00062   mNotesView->setSelectionMode( QIconView::Extended );
00063   mNotesView->setItemsMovable( false );
00064   mNotesView->setResizeMode( QIconView::Adjust );
00065   mNotesView->setAutoArrange( true );
00066   mNotesView->setSorting( true );
00067 
00068   connect( mNotesView, SIGNAL( executed( QIconViewItem* ) ),
00069            this, SLOT( editNote( QIconViewItem* ) ) );
00070   connect( mNotesView, SIGNAL( returnPressed( QIconViewItem* ) ),
00071            this, SLOT( editNote( QIconViewItem* ) ) );
00072   connect( mNotesView, SIGNAL( itemRenamed( QIconViewItem* ) ),
00073            this, SLOT( renamedNote( QIconViewItem* ) ) );
00074   connect( mNotesView, SIGNAL( contextMenuRequested( QIconViewItem*, const QPoint& ) ),
00075            this, SLOT( popupRMB( QIconViewItem*, const QPoint& ) ) );
00076   connect( mNotesView, SIGNAL( onItem( QIconViewItem* ) ),
00077            this, SLOT( slotOnItem( QIconViewItem* ) ) );
00078   connect( mNotesView, SIGNAL( onViewport() ),
00079            this, SLOT( slotOnViewport() ) );
00080   connect( mNotesView, SIGNAL( currentChanged( QIconViewItem* ) ),
00081            this, SLOT( slotOnCurrentChanged( QIconViewItem* ) ) );
00082 
00083   slotOnCurrentChanged( 0 );
00084 
00085   new KParts::SideBarExtension( mNotesView, this, "NotesSideBarExtension" );
00086 
00087   setWidget( mNotesView );
00088   setXMLFile( "knotes_part.rc" );
00089 
00090   // connect the resource manager
00091   connect( mManager, SIGNAL( sigRegisteredNote( KCal::Journal* ) ),
00092            this, SLOT( createNote( KCal::Journal* ) ) );
00093   connect( mManager, SIGNAL( sigDeregisteredNote( KCal::Journal* ) ),
00094            this, SLOT( killNote( KCal::Journal* ) ) );
00095 
00096   // read the notes
00097   mManager->load();
00098 }
00099 
00100 KNotesPart::~KNotesPart()
00101 {
00102   delete mNoteTip;
00103   mNoteTip = 0;
00104 
00105   delete mManager;
00106   mManager = 0;
00107 }
00108 
00109 bool KNotesPart::openFile()
00110 {
00111   return false;
00112 }
00113 
00114 
00115 // public KNotes DCOP interface implementation
00116 
00117 QString KNotesPart::newNote( const QString& name, const QString& text )
00118 {
00119   // create the new note
00120   KCal::Journal *journal = new KCal::Journal();
00121 
00122   // new notes have the current date/time as title if none was given
00123   if ( !name.isEmpty() )
00124       journal->setSummary( name );
00125   else
00126       journal->setSummary( KGlobal::locale()->formatDateTime( QDateTime::currentDateTime() ) );
00127 
00128   // the body of the note
00129   journal->setDescription( text );
00130 
00131 
00132 
00133   // Edit the new note if text is empty
00134   if ( text.isNull() )
00135   {
00136     if ( !mNoteEditDlg )
00137       mNoteEditDlg = new KNoteEditDlg( widget() );
00138 
00139     mNoteEditDlg->setTitle( journal->summary() );
00140     mNoteEditDlg->setText( journal->description() );
00141 
00142     if ( mNoteEditDlg->exec() == QDialog::Accepted )
00143     {
00144       journal->setSummary( mNoteEditDlg->title() );
00145       journal->setDescription( mNoteEditDlg->text() );
00146     }
00147     else
00148     {
00149       delete journal;
00150       return "";
00151     }
00152   }
00153 
00154   mManager->addNewNote( journal );
00155   mManager->save();
00156 
00157   KNotesIconViewItem *note = mNoteList[ journal->uid() ];
00158   mNotesView->ensureItemVisible( note );
00159   mNotesView->setCurrentItem( note );
00160 
00161   return journal->uid();
00162 }
00163 
00164 QString KNotesPart::newNoteFromClipboard( const QString& name )
00165 {
00166   const QString& text = KApplication::clipboard()->text();
00167   return newNote( name, text );
00168 }
00169 
00170 void KNotesPart::killNote( const QString& id )
00171 {
00172   killNote( id, false );
00173 }
00174 
00175 void KNotesPart::killNote( const QString& id, bool force )
00176 {
00177   KNotesIconViewItem *note = mNoteList[ id ];
00178 
00179   if ( note &&
00180        ( (!force && KMessageBox::warningContinueCancelList( mNotesView,
00181                     i18n( "Do you really want to delete this note?" ),
00182                     mNoteList[ id ]->text(), i18n( "Confirm Delete" ),
00183                     KStdGuiItem::del() ) == KMessageBox::Continue)
00184          || force )
00185      )
00186   {
00187     mManager->deleteNote( mNoteList[id]->journal() );
00188     mManager->save();
00189   }
00190 }
00191 
00192 QString KNotesPart::name( const QString& id ) const
00193 {
00194   KNotesIconViewItem *note = mNoteList[ id ];
00195   if ( note )
00196     return note->text();
00197   else
00198     return QString::null;
00199 }
00200 
00201 QString KNotesPart::text( const QString& id ) const
00202 {
00203   KNotesIconViewItem *note = mNoteList[id];
00204   if ( note )
00205     return note->journal()->description();
00206   else
00207     return QString::null;
00208 }
00209 
00210 void KNotesPart::setName( const QString& id, const QString& newName )
00211 {
00212   KNotesIconViewItem *note = mNoteList[ id ];
00213   if ( note ) {
00214     note->setText( newName );
00215     mManager->save();
00216   }
00217 }
00218 
00219 void KNotesPart::setText( const QString& id, const QString& newText )
00220 {
00221   KNotesIconViewItem *note = mNoteList[ id ];
00222   if ( note ) {
00223     note->journal()->setDescription( newText );
00224     mManager->save();
00225   }
00226 }
00227 
00228 QMap<QString, QString> KNotesPart::notes() const
00229 {
00230   QMap<QString, QString> notes;
00231   QDictIterator<KNotesIconViewItem> it( mNoteList );
00232 
00233   for ( ; it.current(); ++it )
00234     notes.insert( (*it)->journal()->uid(), (*it)->journal()->summary() );
00235 
00236   return notes;
00237 }
00238 
00239 
00240 // private stuff
00241 
00242 void KNotesPart::killSelectedNotes()
00243 {
00244   QPtrList<KNotesIconViewItem> items;
00245   QStringList notes;
00246 
00247   KNotesIconViewItem *knivi;
00248   for ( QIconViewItem *it = mNotesView->firstItem(); it; it = it->nextItem() ) {
00249     if ( it->isSelected() ) {
00250       knivi = static_cast<KNotesIconViewItem *>( it );
00251       items.append( knivi );
00252       notes.append( knivi->text() );
00253     }
00254   }
00255 
00256   if ( items.isEmpty() )
00257     return;
00258 
00259   int ret = KMessageBox::warningContinueCancelList( mNotesView,
00260             i18n( "Do you really want to delete this note?",
00261                   "Do you really want to delete these %n notes?", items.count() ),
00262             notes, i18n( "Confirm Delete" ),
00263             KStdGuiItem::del() );
00264 
00265   if ( ret == KMessageBox::Continue ) {
00266     QPtrListIterator<KNotesIconViewItem> kniviIt( items );
00267     while ( (knivi = *kniviIt) ) {
00268       ++kniviIt;
00269       mManager->deleteNote( knivi->journal() );
00270     }
00271 
00272     mManager->save();
00273   }
00274 }
00275 
00276 void KNotesPart::popupRMB( QIconViewItem *item, const QPoint& pos )
00277 {
00278   QPopupMenu *contextMenu = NULL;
00279 
00280   if ( item )
00281     contextMenu = static_cast<QPopupMenu *>( factory()->container( "note_context", this ) );
00282   else
00283     contextMenu = static_cast<QPopupMenu *>( factory()->container( "notepart_context", this ) );
00284 
00285   if ( !contextMenu )
00286     return;
00287 
00288   contextMenu->popup( pos );
00289 }
00290 
00291 void KNotesPart::slotOnItem( QIconViewItem *i )
00292 {
00293   // TODO: disable (i.e. setNote( QString::null )) when mouse button pressed
00294 
00295   KNotesIconViewItem *item = static_cast<KNotesIconViewItem *>( i );
00296   mNoteTip->setNote( item );
00297 }
00298 
00299 void KNotesPart::slotOnViewport()
00300 {
00301   mNoteTip->setNote( 0 );
00302 }
00303 
00304 // TODO: also with takeItem, clear(),
00305 
00306 // create and kill the icon view item corresponding to the note, edit the note
00307 
00308 void KNotesPart::createNote( KCal::Journal *journal )
00309 {
00310   // make sure all fields are existent, initialize them with default values
00311   QString property = journal->customProperty( "KNotes", "BgColor" );
00312   if ( property.isNull() )
00313     journal->setCustomProperty( "KNotes", "BgColor", "#ffff00" );
00314 
00315   property = journal->customProperty( "KNotes", "FgColor" );
00316   if ( property.isNull() )
00317     journal->setCustomProperty( "KNotes", "FgColor", "#000000" );
00318 
00319   property = journal->customProperty( "KNotes", "RichText" );
00320   if ( property.isNull() )
00321     journal->setCustomProperty( "KNotes", "RichText", "true" );
00322 
00323   mNoteList.insert( journal->uid(), new KNotesIconViewItem( mNotesView, journal ) );
00324 }
00325 
00326 void KNotesPart::killNote( KCal::Journal *journal )
00327 {
00328   mNoteList.remove( journal->uid() );
00329 }
00330 
00331 void KNotesPart::editNote( QIconViewItem *item )
00332 {
00333   if ( !mNoteEditDlg )
00334     mNoteEditDlg = new KNoteEditDlg( widget() );
00335 
00336   KCal::Journal *journal = static_cast<KNotesIconViewItem *>( item )->journal();
00337 
00338   mNoteEditDlg->setRichText( journal->customProperty( "KNotes", "RichText" ) == "true" );
00339   mNoteEditDlg->setTitle( journal->summary() );
00340   mNoteEditDlg->setText( journal->description() );
00341 
00342   if ( mNoteEditDlg->exec() == QDialog::Accepted ) {
00343     item->setText( mNoteEditDlg->title() );
00344     journal->setDescription( mNoteEditDlg->text() );
00345     mManager->save();
00346   }
00347 }
00348 
00349 void KNotesPart::renameNote()
00350 {
00351   mNotesView->currentItem()->rename();
00352 }
00353 
00354 void KNotesPart::renamedNote( QIconViewItem* )
00355 {
00356   mManager->save();
00357 }
00358 
00359 void KNotesPart::slotOnCurrentChanged( QIconViewItem* )
00360 {
00361   KAction *renameAction = actionCollection()->action( "edit_rename" );
00362   KAction *deleteAction = actionCollection()->action( "edit_delete" );
00363 
00364   if ( !mNotesView->currentItem() ) {
00365     renameAction->setEnabled( false );
00366     deleteAction->setEnabled( false );
00367   } else {
00368     renameAction->setEnabled( true );
00369     deleteAction->setEnabled( true );
00370   }
00371 }
00372 
00373 #include "knotes_part.moc"
00374 #include "knotes_part_p.moc"
00375 
KDE Home | KDE Accessibility Home | Description of Access Keys