KDevelop API Documentation

knotes_part.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2002 Daniel Molkentin <molkentin@kde.org>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include <qpopupmenu.h>
00021 
00022 #include <kiconloader.h>
00023 #include <kiconview.h>
00024 #include <dcopclient.h>
00025 #include <kapplication.h>
00026 #include <kdebug.h>
00027 #include <klocale.h>
00028 #include <krun.h>
00029 #include <kstandarddirs.h>
00030 #include <kmessagebox.h>
00031 
00032 #include "knotes_part.h"
00033 
00034 class NotesItem : public KIconViewItem
00035 {
00036 public:
00037     NotesItem(KIconView * parent, int id, const QString& text);
00038 
00039     QString id() const { return QString::number( _noteID ); }
00040     int noteID() { return _noteID; };
00041 
00042 private:
00043     int _noteID;
00044 };
00045 
00046 NotesItem::NotesItem(KIconView * parent, int id, const QString& text):
00047     KIconViewItem(parent, text, DesktopIcon("knotes"))
00048 {
00049     _noteID = id;
00050     setRenameEnabled(true);
00051 }
00052 
00053 KNotesPart::KNotesPart(QObject *parent, const char *name)
00054     : KParts::ReadOnlyPart(parent, name),
00055     m_iconView(new KIconView), m_popupMenu(new QPopupMenu)
00056 {
00057 
00058     m_popupMenu->insertItem(BarIcon("editdelete"), i18n("Remove Note"),
00059                 this, SLOT(slotRemoveCurrentNote()));
00060     m_popupMenu->insertItem(BarIcon("editrename"), i18n("Rename Note"),
00061                 this, SLOT(slotRenameCurrentNote()));
00062 
00063     connect(m_iconView, SIGNAL(executed(QIconViewItem*)),
00064         this, SLOT(slotOpenNote(QIconViewItem*)));
00065     connect(m_iconView, SIGNAL(rightButtonClicked(QIconViewItem*, const QPoint&)),
00066         this, SLOT(slotPopupRMB(QIconViewItem*, const QPoint&)));
00067     connect(m_iconView, SIGNAL(itemRenamed(QIconViewItem*, const QString&)),
00068         this, SLOT(slotNoteRenamed(QIconViewItem*, const QString&)));
00069 
00070     initKNotes();
00071     setWidget(m_iconView);
00072 
00073     m_iconView->arrangeItemsInGrid();
00074     m_iconView->setItemsMovable(false);
00075 }
00076 
00077 void KNotesPart::initKNotes()
00078 {
00079     QString *error = 0;
00080     int started = KApplication::startServiceByDesktopName("knotes", QString(), error);
00081 
00082     if ( started > 0 )
00083     {
00084     if (error)
00085         KMessageBox::error(0L, *error, i18n("Error"));
00086     return;
00087     }
00088 
00089     delete error;
00090 
00091     m_iconView->clear();
00092 
00093     NotesMap map;
00094     map = fetchNotes();
00095     NotesMap::const_iterator it;
00096     for (it = map.begin(); it != map.end(); ++it )
00097     {
00098     (void) new NotesItem( m_iconView, it.key(), it.data() );
00099     }
00100 
00101 }
00102 
00103 bool KNotesPart::openFile()
00104 {
00105     return false;
00106 }
00107 
00108 NotesMap KNotesPart::fetchNotes()
00109 {
00110     QCString replyType;
00111     QByteArray data, replyData;
00112     QDataStream arg(  data, IO_WriteOnly );
00113     if( kapp->dcopClient()->call( "knotes", "KNotesIface", "notes()", data, replyType, replyData ) )
00114     {
00115     kdDebug() << "Reply Type: " << replyType << endl;
00116     QDataStream answer(  replyData, IO_ReadOnly );
00117     NotesMap notes;
00118     answer >> notes;
00119     return notes;
00120     }
00121     else
00122     return NotesMap();
00123 
00124 }
00125 
00126 void KNotesPart::slotPopupRMB(QIconViewItem *item, const QPoint& pos)
00127 {
00128     if (!item)
00129     return;
00130 
00131     m_popupMenu->popup(pos);
00132 }
00133 
00134 void KNotesPart::slotRemoveCurrentNote()
00135 {
00136     QIconViewItem* item = m_iconView->currentItem();
00137 
00138     // better safe than sorry
00139     if (!item)
00140     return;
00141 
00142     int id = static_cast<NotesItem*>( item )->noteID();
00143 
00144     QByteArray data;
00145     QDataStream arg( data, IO_WriteOnly );
00146     arg << id;
00147     if ( kapp->dcopClient()->send( "knotes", "KNotesIface", "killNote(int)", data ) )
00148     kdDebug() << "Deleting Note!" << endl;
00149 
00150     // reinit knotes and refetch notes
00151     initKNotes();
00152 }
00153 
00154 void KNotesPart::slotRenameCurrentNote()
00155 {
00156     // better safe than sorry
00157     if(m_iconView->currentItem())
00158     m_iconView->currentItem()->rename();
00159 }
00160 
00161 
00162 void KNotesPart::slotNoteRenamed(QIconViewItem *item, const QString& text)
00163 {
00164     // better safe than sorry
00165     if (!item)
00166     return;
00167 
00168     int id = static_cast<NotesItem*>( item )->noteID();
00169 
00170     QByteArray data;
00171     QDataStream arg( data, IO_WriteOnly );
00172     arg << id;
00173     arg << text;
00174     if ( kapp->dcopClient()->send( "knotes", "KNotesIface", "setName(int, QString)", data ) )
00175     kdDebug() << "Rename Note!" << endl;
00176 
00177     // reinit knotes and refetch notes
00178     initKNotes();
00179 
00180     m_iconView->arrangeItemsInGrid();
00181 }
00182 
00183 void KNotesPart::slotOpenNote( QIconViewItem *item )
00184 {
00185     int id = static_cast<NotesItem*>( item )->noteID();
00186 
00187     QByteArray data;
00188     QDataStream arg( data, IO_WriteOnly );
00189     arg << id;
00190     if ( kapp->dcopClient()->send( "knotes", "KNotesIface", "showNote(int)", data ) )
00191     kdDebug() << "Opening Note!" << id << endl;
00192 }
00193 
00194 void KNotesPart::slotNewNote()
00195 {
00196     kdDebug() << "slotNewNote called!" << endl;
00197     QByteArray data;
00198     QDataStream arg(  data, IO_WriteOnly );
00199     arg << QString::null << QString::null;
00200     if ( !kapp->dcopClient()->send(  "knotes", "KNotesIface", "newNote(QString, QString)", data ) )
00201     KMessageBox::error(0, i18n("Unable to add a new note."));
00202 
00203     // reinit knotes and refetch notes
00204     initKNotes();
00205 }
00206 
00207 
00208 #include "knotes_part.moc"
KDE Logo
This file is part of the documentation for KDevelop Version 3.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 00:03:57 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003