00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
#include <qlayout.h>
00022
#include <qpopupmenu.h>
00023
#include <qtextedit.h>
00024
00025
#include <kaction.h>
00026
#include <kapplication.h>
00027
#include <kdebug.h>
00028
#include <kdialogbase.h>
00029
#include <kiconloader.h>
00030
#include <kinputdialog.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
00038
#include <libkdepim/infoextension.h>
00039
#include <libkdepim/sidebarextension.h>
00040
00041
#include "knotes_part.h"
00042
00043
class NotesItem :
public KListViewItem
00044 {
00045
public:
00046 NotesItem( KListView *parent, KCal::Journal *journal )
00047 : KListViewItem( parent,
"" ), mJournal( journal )
00048 {
00049 setRenameEnabled( 0,
true );
00050 setPixmap( 0, KGlobal::iconLoader()->loadIcon(
"knotes", KIcon::Small ) );
00051 }
00052
00053 KCal::Journal* journal() {
return mJournal; }
00054
00055
virtual void setText(
int column,
const QString &text )
00056 {
00057
if ( column == 0 )
00058 mJournal->setSummary( text );
00059 }
00060
00061
virtual QString text(
int column )
const
00062
{
00063
if ( column == 0 )
00064
return mJournal->summary();
00065
else if ( column == 1 )
00066
return mJournal->description().replace(
"\n",
" " );
00067
else
00068
return QString();
00069 }
00070
00071
private:
00072 KCal::Journal* mJournal;
00073 };
00074
00075
class NoteEditDialog :
public KDialogBase
00076 {
00077
public:
00078 NoteEditDialog(
QWidget *parent,
const QString &text )
00079 : KDialogBase( Plain, i18n(
"Edit Note" ), Ok | Cancel, Ok,
00080 parent, 0, true, true )
00081 {
00082
QWidget *page = plainPage();
00083
QVBoxLayout *layout =
new QVBoxLayout( page );
00084
00085 mTextEdit =
new QTextEdit( page );
00086 layout->addWidget( mTextEdit );
00087
00088 mTextEdit->setText( text );
00089 mTextEdit->setFocus();
00090 }
00091
00092
QString text()
const {
return mTextEdit->text(); }
00093
00094
private:
00095
QTextEdit *mTextEdit;
00096 };
00097
00098 KNotesPart::KNotesPart(
QObject *parent,
const char *name )
00099 : KParts::ReadOnlyPart( parent, name ),
00100 mTicket( 0 ), mPopupMenu( 0 )
00101 {
00102 setInstance(
new KInstance(
"knotes" ) );
00103
00104 mCalendar =
new KCal::CalendarResources;
00105 mResource =
new KCal::ResourceLocal( ::locateLocal(
"data",
"knotes/notes.ics" ) );
00106 mCalendar->resourceManager()->add( mResource );
00107 mCalendar->load();
00108
00109 connect( mCalendar, SIGNAL( calendarChanged() ), SLOT( slotCalendarChanged() ) );
00110
00111 mNotesView =
new KListView();
00112 mNotesView->setSelectionMode( QListView::Extended );
00113 mNotesView->addColumn( i18n(
"Title" ) );
00114 mNotesView->addColumn( i18n(
"Content" ) );
00115 mNotesView->setAllColumnsShowFocus(
true );
00116 mNotesView->setResizeMode( QListView::LastColumn );
00117
00118 (
void)
new KParts::SideBarExtension( mNotesView,
this,
"NotesSideBarExtension" );
00119
00120 (
void)
new KAction( i18n(
"&New..."),
"knotes", CTRL+Key_N,
this, SLOT( newNote() ), actionCollection(),
"file_new" );
00121 mActionEdit =
new KAction( i18n(
"Rename" ),
"editrename",
this,
00122 SLOT( renameNote() ), actionCollection(),
00123
"edit_rename" );
00124 mActionDelete =
new KAction( i18n(
"Delete" ),
"editdelete", 0,
this,
00125 SLOT( removeSelectedNotes() ), actionCollection(),
00126
"edit_delete" );
00127 (
void)
new KAction( i18n(
"Reload" ),
"reload", 0,
this,
00128 SLOT( reloadNotes() ), actionCollection(),
"view_refresh" );
00129
00130 connect( mNotesView, SIGNAL( doubleClicked(
QListViewItem*,
const QPoint&,
int ) ),
00131
this, SLOT( editNote(
QListViewItem*,
const QPoint&,
int ) ) );
00132 connect( mNotesView, SIGNAL( returnPressed(
QListViewItem* ) ),
00133
this, SLOT( editNote(
QListViewItem* ) ) );
00134 connect( mNotesView, SIGNAL( contextMenuRequested(
QListViewItem*,
const QPoint&,
int ) ),
00135
this, SLOT( popupRMB(
QListViewItem*,
const QPoint&,
int ) ) );
00136 connect( mNotesView, SIGNAL( itemRenamed(
QListViewItem*,
int,
const QString& ) ),
00137
this, SLOT( noteRenamed(
QListViewItem*,
int,
const QString& ) ) );
00138
00139 setWidget( mNotesView );
00140
00141 mAppIcon = KGlobal::iconLoader()->loadIcon(
"knotes", KIcon::Small );
00142
00143 KParts::InfoExtension *info =
new KParts::InfoExtension(
this,
"KNoteInfoExtension" );
00144 connect(
this, SIGNAL( noteSelected(
const QString& ) ),
00145 info, SIGNAL( textChanged(
const QString& ) ) );
00146 connect(
this, SIGNAL( noteSelected(
const QPixmap& ) ),
00147 info, SIGNAL( iconChanged(
const QPixmap& ) ) );
00148
00149 setXMLFile(
"knotes_part.rc" );
00150
00151 reloadNotes();
00152 }
00153
00154 KNotesPart::~KNotesPart()
00155 {
00156 }
00157
00158
void KNotesPart::reloadNotes()
00159 {
00160
int pos = mNotesView->itemPos( mNotesView->currentItem() );
00161 mNotesView->clear();
00162
00163 KCal::Journal::List::Iterator it;
00164 KCal::Journal::List notes = mCalendar->journals();
00165
for ( it = notes.begin(); it != notes.end(); ++it )
00166 (
void)
new NotesItem( mNotesView, (*it) );
00167
00168 mNotesView->setCurrentItem( mNotesView->itemAt(
QPoint( 1, pos ) ) );
00169 }
00170
00171
bool KNotesPart::openFile()
00172 {
00173
return false;
00174 }
00175
00176
void KNotesPart::popupRMB(
QListViewItem *item,
const QPoint& pos,
int )
00177 {
00178 mPopupMenu = static_cast<QPopupMenu*>( factory()->container(
"notePopup",
this ) );
00179
if ( !mPopupMenu )
00180
return;
00181
00182
bool state = ( item != 0 );
00183 mActionEdit->setEnabled( state );
00184 mActionDelete->setEnabled( state );
00185
00186 mPopupMenu->popup( pos );
00187 }
00188
00189
void KNotesPart::removeNote()
00190 {
00191 NotesItem *item = static_cast<NotesItem*>( mNotesView->currentItem() );
00192
00193
if ( !item )
00194
return;
00195
00196
if ( !lock() )
00197
return;
00198
00199 mCalendar->deleteJournal( item->journal() );
00200
00201 unlock();
00202 }
00203
00204
void KNotesPart::removeSelectedNotes()
00205 {
00206
QListViewItemIterator it( mNotesView );
00207
QPtrList<NotesItem> items;
00208
QStringList titles;
00209
00210
while ( it.current() ) {
00211
if ( it.current()->isSelected() ) {
00212 NotesItem *item = static_cast<NotesItem*>( it.current() );
00213 items.append( item );
00214 titles.append( item->journal()->summary() );
00215 }
00216
00217 ++it;
00218 }
00219
00220
if ( items.isEmpty() )
00221
return;
00222
00223
if ( !lock() )
00224
return;
00225
00226
int ret = KMessageBox::warningContinueCancelList( mNotesView,
00227 i18n(
"Do you really want to delete this note?",
00228
"Do you really want to delete these %n notes?", items.count() ),
00229 titles,
00230 i18n(
"Confirm Delete" ),
00231 KGuiItem( i18n(
"Delete" ),
"editdelete")
00232 );
00233
00234
if ( ret == KMessageBox::Continue ) {
00235
QPtrListIterator<NotesItem> itemIt( items );
00236 NotesItem *item;
00237
while ( (item = itemIt.current()) != 0 ) {
00238 ++itemIt;
00239
00240 mCalendar->deleteJournal( item->journal() );
00241
00242
delete item;
00243 }
00244 }
00245
00246 unlock();
00247 }
00248
00249
void KNotesPart::renameNote()
00250 {
00251
if ( mNotesView->currentItem() )
00252 mNotesView->currentItem()->startRename( 0 );
00253 }
00254
00255
void KNotesPart::noteRenamed(
QListViewItem *i,
int,
const QString& )
00256 {
00257 NotesItem *item = static_cast<NotesItem*>( i );
00258
00259
if ( !item )
00260
return;
00261
00262
if ( !lock() )
00263
return;
00264
00265 unlock();
00266 }
00267
00268
void KNotesPart::editNote(
QListViewItem *i,
const QPoint&,
int column )
00269 {
00270
if ( column == 1 )
00271 editNote( i );
00272 }
00273
00274
void KNotesPart::editNote(
QListViewItem *i )
00275 {
00276 NotesItem *item = static_cast<NotesItem*>( i );
00277
00278
if ( !item )
00279
return;
00280
00281
if ( !lock() )
00282
return;
00283
00284 NoteEditDialog dlg( mNotesView, item->journal()->description() );
00285
if ( dlg.exec() ) {
00286 item->journal()->setDescription( dlg.text() );
00287 }
00288
00289 unlock();
00290 }
00291
00292
void KNotesPart::newNote()
00293 {
00294
bool ok;
00295
QString title = KInputDialog::getText( i18n(
"New Note" ),
00296 i18n(
"Enter title for the note:" ),
00297 KGlobal::locale()->formatDateTime( QDateTime::currentDateTime() ),
00298 &ok );
00299
if ( !ok )
00300
return;
00301
00302
if ( !lock() )
00303
return;
00304
00305 NoteEditDialog dlg( mNotesView,
"" );
00306
if ( dlg.exec() ) {
00307 KCal::Journal* journal =
new KCal::Journal();
00308 mCalendar->addJournal( journal );
00309 journal->setSummary( title );
00310 journal->setDescription( dlg.text() );
00311 (
void)
new NotesItem( mNotesView, journal );
00312 }
00313
00314 unlock();
00315 }
00316
00317
void KNotesPart::slotCalendarChanged()
00318 {
00319 reloadNotes();
00320 }
00321
00322
bool KNotesPart::lock()
00323 {
00324
if ( mTicket )
00325
return true;
00326
00327 mTicket = mCalendar->requestSaveTicket( mResource );
00328
00329
bool ok = (mTicket != 0);
00330
00331
if ( !ok )
00332 KMessageBox::error( mNotesView,
00333 i18n(
"Unable to access the notes, make sure no other program uses them." ) );
00334
00335
return ok;
00336 }
00337
00338
bool KNotesPart::unlock()
00339 {
00340
if ( !mTicket ) {
00341 kdError() <<
"save with null ticket" << endl;
00342
return false;
00343 }
00344
00345 mCalendar->save( mTicket );
00346 mTicket = 0;
00347
00348
return true;
00349 }
00350
00351
#include "knotes_part.moc"