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