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 
00005    This program is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program; see the file COPYING.  If not, write to
00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #include <qpopupmenu.h>
00022 #include <qsplitter.h>
00023 #include <qtextedit.h>
00024 
00025 #include <dcopclient.h>
00026 #include <dcopref.h>
00027 #include <kaction.h>
00028 #include <kapplication.h>
00029 #include <kdebug.h>
00030 #include <kiconloader.h>
00031 #include <klistview.h>
00032 #include <klocale.h>
00033 #include <kmessagebox.h>
00034 #include <kstandarddirs.h>
00035 #include <kstdaction.h>
00036 #include <kxmlguifactory.h>
00037 #include <libkdepim/infoextension.h>
00038 #include <libkdepim/sidebarextension.h>
00039 
00040 #include "knotes_part.h"
00041 
00042 class NotesItem : public KListViewItem
00043 {
00044   public:
00045     NotesItem( KListView *parent, const QString &id, const QString &text );
00046     QString id() { return noteID; };
00047   private:
00048     QString noteID;
00049 };
00050 
00051 NotesItem::NotesItem( KListView *parent, const QString &id, const QString &text )
00052   : KListViewItem( parent, text )
00053 {
00054   noteID = id;
00055   setRenameEnabled( 0, true );
00056 
00057   setPixmap( 0, KGlobal::iconLoader()->loadIcon( "knotes", KIcon::Small ) );
00058 }
00059 
00060 KNotesPart::KNotesPart( QObject *parent, const char *name )
00061   : KParts::ReadOnlyPart( parent, name ),
00062     mPopupMenu( 0 ),
00063     mNoteChanged( false )
00064 {
00065   setInstance( new KInstance( "knotes" ) );
00066 
00067 
00068   mICal = new KCal::CalendarLocal;
00069   //connect(mICal, SIGNAL(calendarChanged()), SLOT(slotCalendarChanged()));
00070   mICal->load(::locate("data", "knotes/notes.ics"));
00071   mNotes = mICal->journals();
00072 
00073   QSplitter *splitter = new QSplitter( Qt::Horizontal );
00074 
00075   mNotesView = new KListView( splitter );
00076   mNotesView->setSelectionMode( QListView::Extended );
00077   mNotesView->addColumn( i18n( "Title" ) );
00078 
00079   (void) new KParts::SideBarExtension( mNotesView, this, "NotesSideBarExtension" );
00080 
00081   mNotesEdit = new QTextEdit( splitter );
00082 
00083   KStdAction::openNew( this, SLOT( newNote() ), actionCollection() );
00084   mActionEdit = new KAction( i18n( "Rename" ), "editrename", this,
00085                              SLOT( renameNote() ), actionCollection(),
00086                                      "edit_rename" );
00087   mActionDelete = new KAction( i18n( "Delete" ), "editdelete", 0, this,
00088                                SLOT( removeSelectedNotes() ), actionCollection(),
00089                                "edit_delete" );
00090   (void) new KAction( i18n( "Reload" ), "reload", 0, this,
00091                       SLOT( reloadNotes() ), actionCollection(), "view_refresh" );
00092 
00093   connect( mNotesView, SIGNAL( selectionChanged() ),
00094            this, SLOT( showNote() ) );
00095   connect( mNotesView, SIGNAL( contextMenuRequested( QListViewItem*, const QPoint&, int ) ),
00096            this, SLOT( popupRMB( QListViewItem*, const QPoint&, int ) ) );
00097   connect( mNotesView, SIGNAL( itemRenamed( QListViewItem*, int, const QString& ) ),
00098            this, SLOT( noteRenamed( QListViewItem*, int, const QString& ) ) );
00099   connect( mNotesEdit, SIGNAL( textChanged() ),
00100            this, SLOT( noteChanged() ) );
00101 
00102   reloadNotes();
00103   setWidget( splitter );
00104 
00105   mAppIcon = KGlobal::iconLoader()->loadIcon( "knotes", KIcon::Small );
00106 
00107   KParts::InfoExtension *info = new KParts::InfoExtension( this, "KNoteInfoExtension" );
00108   connect( this, SIGNAL( noteSelected( const QString& ) ),
00109            info, SIGNAL( textChanged( const QString& ) ) );
00110   connect( this, SIGNAL( noteSelected( const QPixmap& ) ),
00111            info, SIGNAL( iconChanged( const QPixmap& ) ) );
00112 
00113   setXMLFile( "knotes_part.rc" );
00114 }
00115 
00116 KNotesPart::~KNotesPart()
00117 {
00118   saveNote();
00119 }
00120 
00121 void KNotesPart::reloadNotes()
00122 {
00123   if ( !kapp->dcopClient()->isApplicationRegistered( "knotes" ) ) {
00124     QString *error = 0;
00125     int started  = KApplication::startServiceByDesktopName( "knotes",
00126                                                             QString(), error );
00127 
00128     if ( started > 0 ) {
00129       if ( error )
00130         KMessageBox::error( 0L, *error, i18n( "Error" ) );
00131       return;
00132     }
00133 
00134     delete error;
00135   }
00136 
00137   mNotesView->clear();
00138 
00139   NotesMap map;
00140 
00141   QCString replyType;
00142   QByteArray data, replyData;
00143   QDataStream arg( data, IO_WriteOnly );
00144   if ( kapp->dcopClient()->call( "knotes", "KNotesIface", "notes()", data, replyType, replyData ) ) {
00145     kdDebug(5602) << "Reply Type: " << replyType << endl;
00146     QDataStream answer( replyData, IO_ReadOnly );
00147     answer >> map;
00148   }
00149 
00150   NotesMap::ConstIterator it;
00151   for ( it = map.begin(); it != map.end(); ++it )
00152     (void) new NotesItem( mNotesView, it.key(), it.data() );
00153 
00154   mNotesView->setCurrentItem( mNotesView->firstChild() );
00155   showNote( mNotesView->firstChild() );
00156 }
00157 
00158 bool KNotesPart::openFile()
00159 {
00160   return false;
00161 }
00162 
00163 void KNotesPart::popupRMB( QListViewItem *item, const QPoint& pos, int )
00164 {
00165   mPopupMenu = static_cast<QPopupMenu*>( factory()->container( "notePopup", this ) );
00166   if ( !mPopupMenu )
00167     return;
00168 
00169   bool state = ( item != 0 );
00170   mActionEdit->setEnabled( state );
00171   mActionDelete->setEnabled( state );
00172 
00173   mPopupMenu->popup( pos );
00174 }
00175 
00176 void KNotesPart::removeNote()
00177 {
00178   NotesItem *item = static_cast<NotesItem*>( mNotesView->currentItem() );
00179 
00180   if ( !item )
00181     return;
00182 
00183   DCOPRef dcopCall( "knotes", "KNotesIface" );
00184   dcopCall.call( "killNote(QString, bool)", item->id(), true );
00185 
00186   reloadNotes();
00187 }
00188 
00189 void KNotesPart::removeSelectedNotes()
00190 {
00191   QStringList ids;
00192   QStringList names;
00193 
00194   QListViewItemIterator it( mNotesView );
00195   while ( it.current() ) {
00196     if ( it.current()->isSelected() ) {
00197       ids += static_cast<NotesItem*>( it.current() )->id();
00198       names += it.current()->text( 0 );
00199     }
00200 
00201     ++it;
00202   }
00203 
00204   if ( ids.isEmpty() )
00205     return;
00206 
00207   if ( ids.count() == 1 ) {
00208     DCOPRef dcopCall( "knotes", "KNotesIface" );
00209     dcopCall.call( "killNote(QString)", ids.first() );
00210   } else {
00211     int ret = KMessageBox::warningContinueCancelList( 0,
00212         i18n( "Do you really want to delete that note?", "Do you really want to delete these %n notes?", ids.count() ),
00213         names,
00214         i18n( "Confirm Delete" ),
00215         i18n( "Delete" ) );
00216 
00217     int doIt = ( ret == KMessageBox::Continue );
00218 
00219     if ( doIt )
00220       for ( QStringList::ConstIterator it = ids.begin(); it != ids.end(); ++it ) {
00221         DCOPRef dcopCall( "knotes", "KNotesIface" );
00222         dcopCall.call( "killNote(QString, bool)", *it, true );
00223       }
00224   }
00225 
00226   reloadNotes();
00227 }
00228 
00229 void KNotesPart::renameNote()
00230 {
00231   if ( mNotesView->currentItem() )
00232     mNotesView->currentItem()->startRename( 0 );
00233 }
00234 
00235 void KNotesPart::noteRenamed( QListViewItem *i, int,  const QString& text )
00236 {
00237   NotesItem *item = static_cast<NotesItem*>( i );
00238 
00239   if ( !item )
00240     return;
00241 
00242   DCOPRef dcopCall( "knotes", "KNotesIface" );
00243   dcopCall.send( "setName(QString,QString)", item->id(), text );
00244 }
00245 
00246 void KNotesPart::showNote()
00247 {
00248   showNote( mNotesView->currentItem() );
00249 }
00250 
00251 void KNotesPart::showNote( QListViewItem *i )
00252 {
00253   if ( !mCurrentNote.isEmpty() ) {
00254     if ( mNoteChanged )
00255       saveNote();
00256   }
00257 
00258   mNotesEdit->clear();
00259 
00260   NotesItem *item = static_cast<NotesItem*>( i );
00261   if ( !item ) {
00262     mCurrentNote = "";
00263     return;
00264   }
00265 
00266   mCurrentNote = item->id();
00267 
00268   DCOPRef dcopCall( "knotes", "KNotesIface" );
00269   mNotesEdit->blockSignals( true );
00270   mNotesEdit->setText( dcopCall.call( "text(QString)", item->id() ) );
00271   mNotesEdit->blockSignals( false );
00272 
00273   emit noteSelected( item->text( 0 ) );
00274   emit noteSelected( mAppIcon );
00275 }
00276 
00277 void KNotesPart::noteChanged()
00278 {
00279   mNoteChanged = true;
00280 }
00281 
00282 void KNotesPart::saveNote()
00283 {
00284   if ( mCurrentNote.isEmpty() )
00285     return;
00286 
00287   DCOPRef dcopCall( "knotes", "KNotesIface" );
00288   dcopCall.send( "setText(QString,QString)", mCurrentNote, mNotesEdit->text() );
00289 
00290   mNoteChanged = false;
00291 }
00292 
00293 void KNotesPart::newNote()
00294 {
00295   DCOPRef dcopCall( "knotes", "KNotesIface" );
00296   dcopCall.call( "newNote(QString, QString)", QString::null, QString::null );
00297 
00298   reloadNotes();
00299 }
00300 
00301 #include "knotes_part.moc"
KDE Logo
This file is part of the documentation for kontact Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:39:00 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003