kontact Library API Documentation

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 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., 59 Temple Place - Suite 330,
00019    Boston, MA 02111-1307, 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     m_notesView( new KIconView() ),
00043     m_noteTip( new KNoteTip( m_notesView ) ),
00044     m_noteEditDlg( 0 ),
00045     m_manager( new KNotesResourceManager() )
00046 {
00047     m_noteList.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", 0, 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     m_notesView->setSelectionMode( QIconView::Extended );
00064     m_notesView->setItemsMovable( false );
00065     m_notesView->setResizeMode( QIconView::Adjust );
00066 
00067     connect( m_notesView, SIGNAL(executed( QIconViewItem * )),
00068              this, SLOT(editNote( QIconViewItem * )) );
00069     connect( m_notesView, SIGNAL(returnPressed( QIconViewItem * )),
00070              this, SLOT(editNote( QIconViewItem * )) );
00071     connect( m_notesView, SIGNAL(itemRenamed( QIconViewItem * )),
00072              this, SLOT(renamedNote( QIconViewItem * )) );
00073     connect( m_notesView, SIGNAL(contextMenuRequested( QIconViewItem *, const QPoint & )),
00074              this, SLOT(popupRMB( QIconViewItem *, const QPoint & )) );
00075     connect( m_notesView, SIGNAL(onItem( QIconViewItem * )),
00076              this, SLOT(slotOnItem( QIconViewItem * )) );
00077     connect( m_notesView, SIGNAL(onViewport()), this, SLOT(slotOnViewport()) );
00078 
00079     new KParts::SideBarExtension( m_notesView, this, "NotesSideBarExtension" );
00080 
00081     setWidget( m_notesView );
00082     setXMLFile( "knotes_part.rc" );
00083 
00084     // connect the resource manager
00085     connect( m_manager, SIGNAL(sigRegisteredNote( KCal::Journal * )),
00086              this,      SLOT(createNote( KCal::Journal * )) );
00087     connect( m_manager, SIGNAL(sigDeregisteredNote( KCal::Journal * )),
00088              this,      SLOT(killNote( KCal::Journal * )) );
00089 
00090     // read the notes
00091     m_manager->load();
00092 }
00093 
00094 KNotesPart::~KNotesPart()
00095 {
00096     delete m_noteTip;
00097     delete m_manager;
00098 }
00099 
00100 bool KNotesPart::openFile()
00101 {
00102     return false;
00103 }
00104 
00105 
00106 // public KNotes DCOP interface implementation
00107 
00108 QString KNotesPart::newNote( const QString& name, const QString& text )
00109 {
00110     // create the new note
00111     KCal::Journal *journal = new KCal::Journal();
00112 
00113     // new notes have the current date/time as title if none was given
00114     if ( !name.isEmpty() )
00115         journal->setSummary( name );
00116     else
00117         journal->setSummary( KGlobal::locale()->formatDateTime( QDateTime::currentDateTime() ) );
00118 
00119     // the body of the note
00120     journal->setDescription( text );
00121 
00122     m_manager->addNewNote( journal );
00123 
00124     showNote( journal->uid() );
00125 
00126     m_manager->save();
00127 
00128     return journal->uid();
00129 }
00130 
00131 QString KNotesPart::newNoteFromClipboard( const QString& name )
00132 {
00133     const QString& text = KApplication::clipboard()->text();
00134     return newNote( name, text );
00135 }
00136 
00137 void KNotesPart::showNote( const QString& id ) const
00138 {
00139     KNotesIconViewItem *note = m_noteList[id];
00140     if ( !note )
00141         return;
00142 
00143     m_notesView->ensureItemVisible( note );
00144     m_notesView->setCurrentItem( note );
00145 }
00146 
00147 void KNotesPart::hideNote( const QString& ) const
00148 {
00149     // simply does nothing, there is nothing to hide
00150 }
00151 
00152 void KNotesPart::killNote( const QString& id )
00153 {
00154     killNote( id, false );
00155 }
00156 
00157 void KNotesPart::killNote( const QString& id, bool force )
00158 {
00159     KNotesIconViewItem *note = m_noteList[id];
00160 
00161     if ( note && !force && KMessageBox::warningContinueCancelList( m_notesView,
00162             i18n( "Do you really want to delete this note?" ),
00163             m_noteList[id]->text(), i18n("Confirm Delete"),
00164             KGuiItem( i18n("Delete"), "editdelete" ) ) == KMessageBox::Continue )
00165     {
00166         m_manager->deleteNote( m_noteList[id]->journal() );
00167         m_manager->save();
00168     }
00169 }
00170 
00171 QString KNotesPart::name( const QString& id ) const
00172 {
00173     KNotesIconViewItem *note = m_noteList[id];
00174     if ( note )
00175         return note->text();
00176     else
00177         return QString::null;
00178 }
00179 
00180 QString KNotesPart::text( const QString& id ) const
00181 {
00182     KNotesIconViewItem *note = m_noteList[id];
00183     if ( note )
00184         return note->journal()->description();
00185     else
00186         return QString::null;
00187 }
00188 
00189 void KNotesPart::setName( const QString& id, const QString& newName )
00190 {
00191     KNotesIconViewItem *note = m_noteList[id];
00192     if ( note )
00193     {
00194         note->setText( newName );
00195         m_manager->save();
00196     }
00197 }
00198 
00199 void KNotesPart::setText( const QString& id, const QString& newText )
00200 {
00201     KNotesIconViewItem *note = m_noteList[id];
00202     if ( note )
00203     {
00204         note->journal()->setDescription( newText );
00205         m_manager->save();
00206     }
00207 }
00208 
00209 QMap<QString, QString> KNotesPart::notes() const
00210 {
00211     QMap<QString, QString> notes;
00212     QDictIterator<KNotesIconViewItem> it( m_noteList );
00213 
00214     for ( ; it.current(); ++it )
00215         notes.insert( (*it)->journal()->uid(), (*it)->journal()->description() );
00216 
00217     return notes;
00218 }
00219 
00220 // TODO KDE 4.0: remove
00221 
00222 void KNotesPart::sync( const QString& )
00223 {
00224 }
00225 
00226 bool KNotesPart::isNew( const QString&, const QString& ) const
00227 {
00228     return true;
00229 }
00230 
00231 bool KNotesPart::isModified( const QString&, const QString& ) const
00232 {
00233     return true;
00234 }
00235 
00236 
00237 // private stuff
00238 
00239 void KNotesPart::killSelectedNotes()
00240 {
00241     QPtrList<KNotesIconViewItem> items;
00242     QStringList notes;
00243 
00244     KNotesIconViewItem *knivi;
00245     for ( QIconViewItem *it = m_notesView->firstItem(); it; it = it->nextItem() )
00246     {
00247         if ( it->isSelected() )
00248         {
00249             knivi = static_cast<KNotesIconViewItem *>( it );
00250             items.append( knivi );
00251             notes.append( knivi->text() );
00252         }
00253     }
00254 
00255     if ( items.isEmpty() )
00256         return;
00257 
00258 //   if ( !lock() )
00259 //     return;
00260 
00261     int ret = KMessageBox::warningContinueCancelList( m_notesView,
00262             i18n( "Do you really want to delete this note?",
00263                   "Do you really want to delete these %n notes?", items.count() ),
00264             notes, i18n("Confirm Delete"),
00265             KGuiItem( i18n("Delete"), "editdelete" )
00266                                                     );
00267 
00268     if ( ret == KMessageBox::Continue )
00269     {
00270         QPtrListIterator<KNotesIconViewItem> kniviIt( items );
00271         while ( (knivi = *kniviIt) )
00272         {
00273             ++kniviIt;
00274             m_manager->deleteNote( knivi->journal() );
00275         }
00276         m_manager->save();
00277     }
00278 
00279 //   unlock();
00280 }
00281 
00282 void KNotesPart::popupRMB( QIconViewItem *item, const QPoint& pos )
00283 {
00284   QPopupMenu *contextMenu = static_cast<QPopupMenu *>( factory()->container( "note_context", this ) );
00285 
00286   if ( !contextMenu || !item )
00287     return;
00288 
00289   contextMenu->popup( pos );
00290 }
00291 
00292 void KNotesPart::slotOnItem( QIconViewItem *i )
00293 {
00294     // TODO: disable (i.e. setNote( QString::null )) when mouse button pressed
00295 
00296     KNotesIconViewItem *item = static_cast<KNotesIconViewItem *>(i);
00297     m_noteTip->setNote( item, Qt::AutoText );
00298 }
00299 
00300 void KNotesPart::slotOnViewport()
00301 {
00302     m_noteTip->setNote( 0 );
00303 }
00304 
00305 // TODO: also with takeItem, clear(),
00306 
00307 // create and kill the icon view item corresponding to the note, edit the note
00308 
00309 void KNotesPart::createNote( KCal::Journal *journal )
00310 {
00311     m_noteList.insert( journal->uid(), new KNotesIconViewItem( m_notesView, journal ) );
00312 }
00313 
00314 void KNotesPart::killNote( KCal::Journal *journal )
00315 {
00316     m_noteList.remove( journal->uid() );
00317 }
00318 
00319 void KNotesPart::editNote( QIconViewItem *item )
00320 {
00321     if ( !m_noteEditDlg )
00322         m_noteEditDlg = new KNoteEditDlg( widget() );
00323 
00324     KCal::Journal *journal = static_cast<KNotesIconViewItem *>(item)->journal();
00325     m_noteEditDlg->setText( journal->description() );
00326     if ( m_noteEditDlg->exec() == QDialog::Accepted )
00327         journal->setDescription( m_noteEditDlg->text() );
00328 
00329     m_manager->save();
00330 }
00331 
00332 void KNotesPart::renameNote()
00333 {
00334     m_notesView->currentItem()->rename();
00335 }
00336 
00337 void KNotesPart::renamedNote( QIconViewItem * )
00338 {
00339     m_manager->save();
00340 }
00341 
00342 #include "knotes_part.moc"
00343 #include "knotes_part_p.moc"
KDE Logo
This file is part of the documentation for kontact Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jul 25 11:21:31 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003