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