00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "knotes_part.h"
00024 #include "knotes_part_p.h"
00025 #include "knotetip.h"
00026 #include "knotesadaptor.h"
00027
00028 #include <kactioncollection.h>
00029 #include <kdebug.h>
00030 #include <kaction.h>
00031 #include <kmessagebox.h>
00032 #include <kicon.h>
00033 #include "knotes/knoteprinter.h"
00034 #include "knotes/resourcemanager.h"
00035
00036 #include <QMenu>
00037 #include <QClipboard>
00038 #include <QApplication>
00039
00040 KNotesPart::KNotesPart( QObject *parent )
00041 : KParts::ReadOnlyPart( parent ), mNotesView( new KNotesIconView() ),
00042 mNoteTip( new KNoteTip( mNotesView ) ), mNoteEditDlg( 0 ),
00043 mManager( new KNotesResourceManager() )
00044 {
00045 (void) new KNotesAdaptor( this );
00046 QDBusConnection::sessionBus().registerObject( "/KNotes", this );
00047 mNoteList.setAutoDelete( true );
00048
00049 setComponentData( KComponentData( "knotes" ) );
00050
00051
00052 KAction *action = new KAction( KIcon( "knotes" ), i18n( "&New" ), this );
00053 actionCollection()->addAction( "file_new", action );
00054 connect( action, SIGNAL(triggered(bool)), SLOT(newNote()) );
00055 action->setShortcut( QKeySequence( Qt::CTRL + Qt::Key_N ) );
00056
00057 action = new KAction( KIcon( "edit-rename" ), i18n( "Rename..." ), this );
00058 actionCollection()->addAction( "edit_rename", action );
00059 connect( action, SIGNAL(triggered(bool)), SLOT(renameNote()) );
00060
00061 action = new KAction( KIcon( "edit-delete" ), i18n( "Delete" ), this );
00062 actionCollection()->addAction( "edit_delete", action );
00063 connect( action, SIGNAL(triggered(bool)), SLOT(killSelectedNotes()) );
00064 action->setShortcut( QKeySequence( Qt::Key_Delete ) );
00065
00066 action = new KAction( KIcon( "document-print" ), i18n( "Print Selected Notes..." ), this );
00067 actionCollection()->addAction( "print_note", action );
00068 connect( action, SIGNAL(triggered(bool)), SLOT(printSelectedNotes()) );
00069
00070 action->setShortcut( QKeySequence( Qt::CTRL + Qt::Key_Delete ) );
00071
00072
00073
00074
00075 mNotesView->setSelectionMode( Q3IconView::Extended );
00076 mNotesView->setItemsMovable( false );
00077 mNotesView->setResizeMode( Q3IconView::Adjust );
00078 mNotesView->setSorting( true );
00079
00080 connect( mNotesView, SIGNAL(executed(Q3IconViewItem*)),
00081 this, SLOT(editNote(Q3IconViewItem*)) );
00082 connect( mNotesView, SIGNAL(returnPressed(Q3IconViewItem*)),
00083 this, SLOT(editNote(Q3IconViewItem*)) );
00084 connect( mNotesView, SIGNAL(itemRenamed(Q3IconViewItem*)),
00085 this, SLOT(renamedNote(Q3IconViewItem*)) );
00086 connect( mNotesView, SIGNAL(contextMenuRequested(Q3IconViewItem*,const QPoint&)),
00087 this, SLOT(popupRMB(Q3IconViewItem*,const QPoint&)) );
00088 connect( mNotesView, SIGNAL(onItem(Q3IconViewItem*)),
00089 this, SLOT(slotOnItem(Q3IconViewItem*)) );
00090 connect( mNotesView, SIGNAL(onViewport()),
00091 this, SLOT(slotOnViewport()) );
00092 connect( mNotesView, SIGNAL(currentChanged(Q3IconViewItem*)),
00093 this, SLOT(slotOnCurrentChanged(Q3IconViewItem*)) );
00094
00095 slotOnCurrentChanged( 0 );
00096
00097 setWidget( mNotesView );
00098 setXMLFile( "knotes_part.rc" );
00099
00100
00101 connect( mManager, SIGNAL(sigRegisteredNote(KCal::Journal*)),
00102 this, SLOT(createNote(KCal::Journal*)) );
00103 connect( mManager, SIGNAL(sigDeregisteredNote(KCal::Journal*)),
00104 this, SLOT(killNote(KCal::Journal*)) );
00105
00106
00107 mManager->load();
00108 }
00109
00110 KNotesPart::~KNotesPart()
00111 {
00112 delete mNoteTip;
00113 mNoteTip = 0;
00114
00115 delete mManager;
00116 mManager = 0;
00117 }
00118
00119 void KNotesPart::printSelectedNotes()
00120 {
00121 QList<KCal::Journal*> journals;
00122
00123 for ( Q3IconViewItem *it = mNotesView->firstItem(); it; it = it->nextItem() ) {
00124 if ( it->isSelected() ) {
00125 journals.append( static_cast<KNotesIconViewItem *>( it )->journal() );
00126 }
00127 }
00128
00129 if ( journals.isEmpty() ) {
00130 KMessageBox::information(
00131 mNotesView,
00132 i18n( "To print notes, first select the notes to print from the list." ),
00133 i18n( "Print Popup Notes" ) );
00134 return;
00135 }
00136
00137 KNotePrinter printer;
00138 printer.printNotes( journals );
00139
00140 }
00141
00142 bool KNotesPart::openFile()
00143 {
00144 return false;
00145 }
00146
00147
00148
00149 QString KNotesPart::newNote( const QString &name, const QString &text )
00150 {
00151
00152 KCal::Journal *journal = new KCal::Journal();
00153
00154
00155 if ( !name.isEmpty() ) {
00156 journal->setSummary( name );
00157 } else {
00158 journal->setSummary( KGlobal::locale()->formatDateTime( QDateTime::currentDateTime() ) );
00159 }
00160
00161
00162 journal->setDescription( text );
00163
00164
00165 if ( text.isNull() ) {
00166 if ( !mNoteEditDlg ) {
00167 mNoteEditDlg = new KNoteEditDlg( widget() );
00168 }
00169
00170 mNoteEditDlg->setTitle( journal->summary() );
00171 mNoteEditDlg->setText( journal->description() );
00172
00173 if ( mNoteEditDlg->exec() == QDialog::Accepted ) {
00174 journal->setSummary( mNoteEditDlg->title() );
00175 journal->setDescription( mNoteEditDlg->text() );
00176 } else {
00177 delete journal;
00178 return "";
00179 }
00180 }
00181
00182 mManager->addNewNote( journal );
00183 mManager->save();
00184
00185 KNotesIconViewItem *note = mNoteList[ journal->uid() ];
00186 mNotesView->ensureItemVisible( note );
00187 mNotesView->setCurrentItem( note );
00188
00189 return journal->uid();
00190 }
00191
00192 QString KNotesPart::newNoteFromClipboard( const QString &name )
00193 {
00194 const QString &text = QApplication::clipboard()->text();
00195 return newNote( name, text );
00196 }
00197
00198 void KNotesPart::killNote( const QString &id )
00199 {
00200 killNote( id, false );
00201 }
00202
00203 void KNotesPart::killNote( const QString &id, bool force )
00204 {
00205 KNotesIconViewItem *note = mNoteList[ id ];
00206
00207 if ( note &&
00208 ( (!force && KMessageBox::warningContinueCancelList(
00209 mNotesView,
00210 i18n( "Do you really want to delete this note?" ),
00211 QStringList( mNoteList[ id ]->text() ), i18n( "Confirm Delete" ),
00212 KStandardGuiItem::del() ) == KMessageBox::Continue )
00213 || force ) ) {
00214 mManager->deleteNote( mNoteList[id]->journal() );
00215 mManager->save();
00216 }
00217 }
00218
00219 QString KNotesPart::name( const QString &id ) const
00220 {
00221 KNotesIconViewItem *note = mNoteList[ id ];
00222 if ( note ) {
00223 return note->text();
00224 } else {
00225 return QString();
00226 }
00227 }
00228
00229 QString KNotesPart::text( const QString &id ) const
00230 {
00231 KNotesIconViewItem *note = mNoteList[ id ];
00232 if ( note ) {
00233 return note->journal()->description();
00234 } else {
00235 return QString();
00236 }
00237 }
00238
00239 void KNotesPart::setName( const QString &id, const QString &newName )
00240 {
00241 KNotesIconViewItem *note = mNoteList[ id ];
00242 if ( note ) {
00243 note->setText( newName );
00244 mManager->save();
00245 }
00246 }
00247
00248 void KNotesPart::setText( const QString &id, const QString &newText )
00249 {
00250 KNotesIconViewItem *note = mNoteList[ id ];
00251 if ( note ) {
00252 note->journal()->setDescription( newText );
00253 mManager->save();
00254 }
00255 }
00256
00257 QMap<QString, QString> KNotesPart::notes() const
00258 {
00259 QMap<QString, QString> notes;
00260 Q3DictIterator<KNotesIconViewItem> it( mNoteList );
00261
00262 for ( ; it.current(); ++it ) {
00263 notes.insert( (*it)->journal()->uid(), (*it)->journal()->summary() );
00264 }
00265
00266 return notes;
00267 }
00268
00269
00270
00271 void KNotesPart::killSelectedNotes()
00272 {
00273 QList<KNotesIconViewItem*> items;
00274 QStringList notes;
00275
00276 KNotesIconViewItem *knivi;
00277 for ( Q3IconViewItem *it = mNotesView->firstItem(); it; it = it->nextItem() ) {
00278 if ( it->isSelected() ) {
00279 knivi = static_cast<KNotesIconViewItem *>( it );
00280 items.append( knivi );
00281 notes.append( knivi->text() );
00282 }
00283 }
00284
00285 if ( items.isEmpty() ) {
00286 return;
00287 }
00288
00289 int ret = KMessageBox::warningContinueCancelList(
00290 mNotesView,
00291 i18np( "Do you really want to delete this note?",
00292 "Do you really want to delete these %1 notes?", items.count() ),
00293 notes, i18n( "Confirm Delete" ),
00294 KStandardGuiItem::del() );
00295
00296 if ( ret == KMessageBox::Continue ) {
00297 QListIterator<KNotesIconViewItem*> kniviIt( items );
00298 while ( kniviIt.hasNext() ) {
00299 mManager->deleteNote( kniviIt.next()->journal() );
00300 }
00301
00302 mManager->save();
00303 }
00304 }
00305
00306 void KNotesPart::popupRMB( Q3IconViewItem *item, const QPoint &pos )
00307 {
00308 QMenu *contextMenu = 0;
00309
00310 if ( item ) {
00311 contextMenu = static_cast<QMenu *>( factory()->container( "note_context", this ) );
00312 } else {
00313 contextMenu = static_cast<QMenu *>( factory()->container( "notepart_context", this ) );
00314 }
00315
00316 if ( !contextMenu ) {
00317 return;
00318 }
00319
00320 contextMenu->popup( pos );
00321 }
00322
00323 void KNotesPart::slotOnItem( Q3IconViewItem *i )
00324 {
00325
00326
00327 KNotesIconViewItem *item = static_cast<KNotesIconViewItem *>( i );
00328 mNoteTip->setNote( item );
00329 }
00330
00331 void KNotesPart::slotOnViewport()
00332 {
00333 mNoteTip->setNote( 0 );
00334 }
00335
00336
00337
00338
00339
00340 void KNotesPart::createNote( KCal::Journal *journal )
00341 {
00342
00343 QString property = journal->customProperty( "KNotes", "BgColor" );
00344 if ( property.isNull() ) {
00345 journal->setCustomProperty( "KNotes", "BgColor", "#ffff00" );
00346 }
00347
00348 property = journal->customProperty( "KNotes", "FgColor" );
00349 if ( property.isNull() ) {
00350 journal->setCustomProperty( "KNotes", "FgColor", "#000000" );
00351 }
00352
00353 property = journal->customProperty( "KNotes", "RichText" );
00354 if ( property.isNull() ) {
00355 journal->setCustomProperty( "KNotes", "RichText", "true" );
00356 }
00357
00358 mNoteList.insert( journal->uid(), new KNotesIconViewItem( mNotesView, journal ) );
00359 }
00360
00361 void KNotesPart::killNote( KCal::Journal *journal )
00362 {
00363 mNoteList.remove( journal->uid() );
00364 }
00365
00366 void KNotesPart::editNote( Q3IconViewItem *item )
00367 {
00368 if ( !mNoteEditDlg ) {
00369 mNoteEditDlg = new KNoteEditDlg( widget() );
00370 }
00371
00372 KCal::Journal *journal = static_cast<KNotesIconViewItem *>( item )->journal();
00373 mNoteEditDlg->setTitle( journal->summary() );
00374 mNoteEditDlg->setText( journal->description() );
00375
00376 if ( mNoteEditDlg->exec() == QDialog::Accepted ) {
00377 item->setText( mNoteEditDlg->title() );
00378 journal->setDescription( mNoteEditDlg->text() );
00379 mManager->save();
00380 }
00381 }
00382
00383 void KNotesPart::renameNote()
00384 {
00385 mNotesView->currentItem()->rename();
00386 }
00387
00388 void KNotesPart::renamedNote( Q3IconViewItem * )
00389 {
00390 mManager->save();
00391 }
00392
00393 void KNotesPart::slotOnCurrentChanged( Q3IconViewItem * )
00394 {
00395 QAction *renameAction = actionCollection()->action( "edit_rename" );
00396 QAction *deleteAction = actionCollection()->action( "edit_delete" );
00397
00398 if ( !mNotesView->currentItem() ) {
00399 renameAction->setEnabled( false );
00400 deleteAction->setEnabled( false );
00401 } else {
00402 renameAction->setEnabled( true );
00403 deleteAction->setEnabled( true );
00404 }
00405 }
00406
00407 #include "knotes_part.moc"
00408 #include "knotes_part_p.moc"
00409