26 #include "knotesadaptor.h"
31 #include "knotes/configdialog/knoteconfigdialog.h"
32 #include "knotes/network/knotesnetrecv.h"
33 #include "knotes/print/knoteprinter.h"
34 #include "knotes/print/knoteprintobject.h"
35 #include "knotes/print/knoteprintselectthemedialog.h"
36 #include "knotes/resource/resourcemanager.h"
37 #include "knotes/knoteedit.h"
38 #include "knotes/knotesglobalconfig.h"
39 #include "knotes/configdialog/knotesimpleconfigdialog.h"
40 #include "utils/knoteutils.h"
41 #include "alarms/knotealarmdialog.h"
42 #include "alarms/knotesalarm.h"
43 #include <KCal/Journal>
46 #include <KActionCollection>
48 #include <KInputDialog>
49 #include <KMessageBox>
50 #include <KXMLGUIFactory>
51 #include <KPrintPreview>
52 #include <ksocketfactory.h>
53 #include <KApplication>
54 #include <KFileDialog>
55 #include <KToggleAction>
57 #include <QApplication>
64 #include <dnssd/publicservice.h>
67 : KParts::ReadOnlyPart( parent ),
69 mNoteTip( new
KNoteTip( mNotesWidget->notesView() ) ),
76 (void)
new KNotesAdaptor(
this );
77 QDBusConnection::sessionBus().registerObject( QLatin1String(
"/KNotes"),
this );
79 setComponentData( KComponentData(
"knotes" ) );
82 mNewNote =
new KAction( KIcon( QLatin1String(
"knotes") ),
83 i18nc(
"@action:inmenu create new popup note",
"&New" ),
this );
84 actionCollection()->addAction( QLatin1String(
"file_new"), mNewNote );
85 connect( mNewNote, SIGNAL(triggered(
bool)), SLOT(
newNote()) );
86 mNewNote->setShortcut( QKeySequence( Qt::CTRL + Qt::Key_N ) );
87 mNewNote->setHelpText(
88 i18nc(
"@info:status",
"Create a new popup note" ) );
89 mNewNote->setWhatsThis(
90 i18nc(
"@info:whatsthis",
91 "You will be presented with a dialog where you can add a new popup note." ) );
93 mNoteEdit =
new KAction( KIcon( QLatin1String(
"document-edit") ),
94 i18nc(
"@action:inmenu",
"Edit..." ),
this );
95 actionCollection()->addAction( QLatin1String(
"edit_note"), mNoteEdit );
96 connect( mNoteEdit, SIGNAL(triggered(
bool)), SLOT(editNote()) );
97 mNoteEdit->setHelpText(
98 i18nc(
"@info:status",
"Edit popup note" ) );
99 mNoteEdit->setWhatsThis(
100 i18nc(
"@info:whatsthis",
101 "You will be presented with a dialog where you can modify an existing popup note." ) );
103 mNoteRename =
new KAction( KIcon( QLatin1String(
"edit-rename") ),
104 i18nc(
"@action:inmenu",
"Rename..." ),
this );
105 actionCollection()->addAction( QLatin1String(
"edit_rename"), mNoteRename );
106 connect( mNoteRename, SIGNAL(triggered(
bool)), SLOT(renameNote()) );
107 mNoteRename->setHelpText(
108 i18nc(
"@info:status",
"Rename popup note" ) );
109 mNoteRename->setWhatsThis(
110 i18nc(
"@info:whatsthis",
111 "You will be presented with a dialog where you can rename an existing popup note." ) );
113 mNoteDelete =
new KAction( KIcon( QLatin1String(
"edit-delete") ),
114 i18nc(
"@action:inmenu",
"Delete" ),
this );
115 actionCollection()->addAction( QLatin1String(
"edit_delete"), mNoteDelete );
116 connect( mNoteDelete, SIGNAL(triggered(
bool)), SLOT(killSelectedNotes()) );
117 mNoteDelete->setShortcut( QKeySequence( Qt::Key_Delete ) );
118 mNoteDelete->setHelpText(
119 i18nc(
"@info:status",
"Delete popup note" ) );
120 mNoteDelete->setWhatsThis(
121 i18nc(
"@info:whatsthis",
122 "You will be prompted if you really want to permanently remove "
123 "the selected popup note." ) );
125 mNotePrint =
new KAction( KIcon( QLatin1String(
"document-print") ),
126 i18nc(
"@action:inmenu",
"Print Selected Notes..." ),
this );
127 actionCollection()->addAction( QLatin1String(
"print_note"), mNotePrint );
128 connect( mNotePrint, SIGNAL(triggered(
bool)), SLOT(slotPrintSelectedNotes()) );
129 mNotePrint->setHelpText(
130 i18nc(
"@info:status",
"Print popup note" ) );
131 mNotePrint->setWhatsThis(
132 i18nc(
"@info:whatsthis",
133 "You will be prompted to print the selected popup note." ) );
135 if(KPrintPreview::isAvailable()) {
137 mNotePrintPreview =
new KAction( KIcon( QLatin1String(
"document-print-preview") ),i18nc(
"@action:inmenu",
"Print Preview Selected Notes..." ),
this );
138 actionCollection()->addAction( QLatin1String(
"print_preview_note"), mNotePrintPreview );
140 connect( mNotePrintPreview, SIGNAL(triggered(
bool)), SLOT(slotPrintPreviewSelectedNotes()) );
143 mNoteConfigure =
new KAction( KIcon( QLatin1String(
"configure") ), i18n(
"Note settings..." ),
this );
144 actionCollection()->addAction( QLatin1String(
"configure_note"), mNoteConfigure );
145 connect( mNoteConfigure, SIGNAL(triggered(
bool)), SLOT(slotNotePreferences()) );
147 KAction *act =
new KAction( KIcon( QLatin1String(
"configure") ), i18n(
"Preferences KNotes..." ),
this );
148 actionCollection()->addAction( QLatin1String(
"knotes_configure"), act );
149 connect( act, SIGNAL(triggered(
bool)), SLOT(slotPreferences()) );
151 mNoteSendMail =
new KAction( KIcon( QLatin1String(
"mail-send") ), i18n(
"Mail..." ),
this );
152 actionCollection()->addAction( QLatin1String(
"mail_note"), mNoteSendMail );
153 connect( mNoteSendMail, SIGNAL(triggered(
bool)), SLOT(slotMail()) );
155 mNoteSendNetwork =
new KAction( KIcon( QLatin1String(
"network-wired") ), i18n(
"Send..." ),
this );
156 actionCollection()->addAction( QLatin1String(
"send_note"), mNoteSendNetwork );
157 connect( mNoteSendNetwork, SIGNAL(triggered(
bool)), SLOT(slotSendToNetwork()) );
159 mNoteSetAlarm =
new KAction( KIcon( QLatin1String(
"knotes_alarm") ), i18n(
"Set Alarm..." ),
this );
160 actionCollection()->addAction( QLatin1String(
"set_alarm"), mNoteSetAlarm );
161 connect( mNoteSetAlarm, SIGNAL(triggered(
bool)), SLOT(slotSetAlarm()) );
163 act =
new KAction( KIcon( QLatin1String(
"edit-paste") ),
164 i18n(
"New Note From Clipboard" ),
this );
165 actionCollection()->addAction( QLatin1String(
"new_note_clipboard"), act );
166 connect( act, SIGNAL(triggered()), SLOT(slotNewNoteFromClipboard()) );
169 mSaveAs =
new KAction( KIcon( QLatin1String(
"document-save-as") ), i18n(
"Save As..." ),
this );
170 actionCollection()->addAction( QLatin1String(
"save_note"), mSaveAs );
171 connect( mSaveAs, SIGNAL(triggered(
bool)), SLOT(slotSaveAs()) );
173 mReadOnly =
new KToggleAction( KIcon( QLatin1String(
"object-locked") ), i18n(
"Lock" ),
this );
174 actionCollection()->addAction( QLatin1String(
"lock_note"), mReadOnly );
175 connect( mReadOnly, SIGNAL(triggered(
bool)), SLOT(slotUpdateReadOnly()) );
176 mReadOnly->setCheckedState( KGuiItem( i18n(
"Unlock" ), QLatin1String(
"object-unlocked") ) );
186 connect( mNotesWidget->
notesView(), SIGNAL(entered(QModelIndex)),
187 this, SLOT(requestToolTip(QModelIndex)));
189 connect( mNotesWidget->
notesView(), SIGNAL(viewportEntered()),
190 this, SLOT(hideToolTip()));
192 connect( mNotesWidget->
notesView(), SIGNAL(itemSelectionChanged()),
193 this, SLOT(slotOnCurrentChanged()) );
195 slotOnCurrentChanged();
197 setWidget( mNotesWidget );
198 setXMLFile( QLatin1String(
"knotes_part.rc") );
201 connect( mManager, SIGNAL(sigRegisteredNote(KCal::Journal*)),
202 this, SLOT(createNote(KCal::Journal*)) );
203 connect( mManager, SIGNAL(sigDeregisteredNote(KCal::Journal*)),
204 this, SLOT(
killNote(KCal::Journal*)) );
206 mAlarm =
new KNotesAlarm( mManager,
this );
207 updateNetworkListener();
224 QHashIterator<QString, KNotesIconViewItem*> i(mNoteList);
225 while ( i.hasNext() ) {
227 notes.append(i.value()->journal()->uid());
232 void KNotesPart::requestToolTip(
const QModelIndex &index )
234 const QRect m_itemRect = mNotesWidget->
notesView()->visualRect( index );
236 static_cast<KNotesIconViewItem *>( mNotesWidget->
notesView()->itemAt( m_itemRect.topLeft() ) ) );
239 void KNotesPart::hideToolTip()
244 void KNotesPart::slotPrintPreviewSelectedNotes()
246 printSelectedNotes(
true);
249 void KNotesPart::slotPrintSelectedNotes()
251 printSelectedNotes(
false);
254 void KNotesPart::printSelectedNotes(
bool preview)
257 if ( lst.isEmpty() ) {
258 KMessageBox::information(
261 "To print notes, first select the notes to print from the list." ),
262 i18nc(
"@title:window",
"Print Popup Notes" ) );
266 KNotesGlobalConfig *globalConfig = KNotesGlobalConfig::self();
267 QString printingTheme = globalConfig->theme();
268 if (printingTheme.isEmpty()) {
269 QPointer<KNotePrintSelectThemeDialog> dlg =
new KNotePrintSelectThemeDialog(widget());
271 printingTheme = dlg->selectedTheme();
275 if (!printingTheme.isEmpty()) {
279 listPrintObj.append(
new KNotePrintObject(static_cast<KNotesIconViewItem *>( item )->journal()));
281 KNotePrinter printer;
282 printer.printNotes( listPrintObj, printingTheme, preview );
283 qDeleteAll(listPrintObj);
297 Journal *journal =
new Journal();
300 if ( !name.isEmpty() ) {
301 journal->setSummary( name );
303 journal->setSummary( KGlobal::locale()->formatDateTime( QDateTime::currentDateTime() ) );
307 journal->setDescription( text );
310 if ( text.isNull() ) {
311 QPointer<KNoteEditDialog> dlg =
new KNoteEditDialog(
false, widget() );
313 dlg->setTitle( journal->summary() );
314 dlg->setText( journal->description() );
317 const QString
property = journal->customProperty(
"KNotes",
"RichText");
318 if ( !property.isNull() ) {
319 dlg->setAcceptRichText( property == QLatin1String(
"true") ?
true :
false );
321 KNotesGlobalConfig *globalConfig = KNotesGlobalConfig::self();
322 dlg->setAcceptRichText( globalConfig->richText());
326 dlg->noteEdit()->setFocus();
327 if ( dlg->exec() == QDialog::Accepted ) {
328 journal->setSummary( dlg->title() );
329 journal->setDescription( dlg->text() );
338 mManager->addNewNote( journal );
341 mNotesWidget->
notesView()->scrollToItem( note );
342 mNotesWidget->
notesView()->setCurrentItem( note );
344 return journal->uid();
349 const QString &
text = QApplication::clipboard()->text();
363 ( (!force && KMessageBox::warningContinueCancelList(
365 i18nc(
"@info",
"Do you really want to delete this note?" ),
366 QStringList( mNoteList.value(
id )->text() ),
367 i18nc(
"@title:window",
"Confirm Delete" ),
368 KStandardGuiItem::del() ) == KMessageBox::Continue )
370 KNoteUtils::removeNote(mNoteList.value(
id )->journal(), 0);
371 mManager->deleteNote( mNoteList.value(
id )->journal() );
390 return note->
journal()->description();
409 note->
journal()->setDescription( newText );
416 QMap<QString, QString>
notes;
418 QHashIterator<QString, KNotesIconViewItem*> i(mNoteList);
419 while ( i.hasNext() ) {
421 notes.insert( i.value()->journal()->uid(), i.value()->journal()->summary() );
428 void KNotesPart::killSelectedNotes()
431 if ( lst.isEmpty() ) {
438 items.append( knivi );
446 QListIterator<KNotesIconViewItem*> kniviIt( items );
447 while ( kniviIt.hasNext() ) {
450 Journal *journal = iconViewIcon->
journal();
451 KNoteUtils::removeNote(journal, 0);
452 mManager->deleteNote( journal );
466 if ( mNotesWidget->
notesView()->itemAt ( pos ) ) {
467 contextMenu->addAction(mNewNote);
468 const bool uniqueNoteSelected = (mNotesWidget->
notesView()->selectedItems().count() == 1);
469 const bool readOnly = uniqueNoteSelected ?
static_cast<KNotesIconViewItem *
>(mNotesWidget->
notesView()->selectedItems().at(0))->readOnly() :
false;
470 if (uniqueNoteSelected) {
472 contextMenu->addSeparator();
473 contextMenu->addAction(mNoteSetAlarm);
475 contextMenu->addSeparator();
476 contextMenu->addAction(mSaveAs);
477 contextMenu->addSeparator();
478 contextMenu->addAction(mNoteEdit);
479 contextMenu->addAction(mReadOnly);
481 contextMenu->addAction(mNoteRename);
482 contextMenu->addSeparator();
483 contextMenu->addAction(mNoteSendMail);
484 contextMenu->addSeparator();
485 contextMenu->addAction(mNoteSendNetwork);
487 contextMenu->addSeparator();
488 contextMenu->addAction(mNotePrint);
489 contextMenu->addAction(mNotePrintPreview);
492 contextMenu->addSeparator();
493 contextMenu->addAction(mNoteConfigure);
494 contextMenu->addSeparator();
495 contextMenu->addAction(mNoteDelete);
498 contextMenu->addAction(mNewNote);
501 contextMenu->exec( mNotesWidget->
notesView()->mapFromParent( globalPos ) );
509 void KNotesPart::createNote( KCal::Journal *journal )
524 Journal *journal = knotesItem->
journal();
525 dlg->setTitle( journal->summary() );
526 dlg->setText( journal->description() );
528 const QString
property = journal->customProperty(
"KNotes",
"RichText");
529 if ( !property.isNull() ) {
530 dlg->setAcceptRichText( property == QLatin1String(
"true") ?
true :
false );
532 KNotesGlobalConfig *globalConfig = KNotesGlobalConfig::self();
533 dlg->setAcceptRichText( globalConfig->richText());
535 dlg->setTabSize(knotesItem->
tabSize());
536 dlg->setAutoIndentMode(knotesItem->
autoIndent());
537 dlg->setTextFont(knotesItem->
textFont());
539 dlg->noteEdit()->setFocus();
540 if ( dlg->exec() == QDialog::Accepted ) {
542 journal->setDescription( dlg->text() );
548 void KNotesPart::editNote()
556 void KNotesPart::renameNote()
560 const QString oldName = knoteItem->
realName();
562 const QString newName =
563 KInputDialog::getText( i18nc(
"@title:window",
"Rename Popup Note" ),
564 i18nc(
"@label:textbox",
"New Name:" ),
565 oldName, &ok, mNotesWidget );
566 if ( ok && ( newName != oldName ) ) {
572 void KNotesPart::slotOnCurrentChanged( )
574 const bool uniqueNoteSelected = (mNotesWidget->
notesView()->selectedItems().count() == 1);
575 const bool enabled(mNotesWidget->
notesView()->currentItem());
576 mNoteRename->setEnabled( enabled && uniqueNoteSelected);
577 mNoteEdit->setEnabled( enabled && uniqueNoteSelected);
578 mNoteConfigure->setEnabled( uniqueNoteSelected );
579 mNoteSendMail->setEnabled(uniqueNoteSelected);
580 mNoteSendNetwork->setEnabled(uniqueNoteSelected);
581 mNoteSetAlarm->setEnabled(uniqueNoteSelected);
582 mSaveAs->setEnabled(uniqueNoteSelected);
583 mReadOnly->setEnabled(uniqueNoteSelected);
584 if (uniqueNoteSelected) {
586 mReadOnly->setChecked(readOnly);
587 mNoteEdit->setText(readOnly ? i18n(
"Show Note...") : i18nc(
"@action:inmenu",
"Edit..." ));
589 mNoteEdit->setText(i18nc(
"@action:inmenu",
"Edit..." ));
593 void KNotesPart::slotNotePreferences()
595 if (!mNotesWidget->
notesView()->currentItem())
600 QPointer<KNoteSimpleConfigDialog> dialog =
new KNoteSimpleConfigDialog( knoteItem->
config(),
name, widget(), knoteItem->
journal()->uid() );
601 connect( dialog, SIGNAL(settingsChanged(QString)) ,
this,
602 SLOT(slotApplyConfig()) );
607 void KNotesPart::slotApplyConfig()
614 void KNotesPart::slotPreferences()
617 KNoteConfigDialog *dialog =
new KNoteConfigDialog( i18n(
"Settings" ), widget());
618 connect( dialog, SIGNAL(configWrote()),
this, SLOT(slotConfigUpdated()));
622 void KNotesPart::slotConfigUpdated()
624 updateNetworkListener();
627 void KNotesPart::slotMail()
629 if (!mNotesWidget->
notesView()->currentItem())
632 KNoteUtils::sendMail(widget(),knoteItem->
realName(), knoteItem->
journal()->description());
635 void KNotesPart::slotSendToNetwork()
637 if (!mNotesWidget->
notesView()->currentItem())
640 KNoteUtils::sendToNetwork(widget(),knoteItem->
realName(), knoteItem->
journal()->description());
643 void KNotesPart::updateNetworkListener()
650 if ( KNotesGlobalConfig::receiveNotes() ) {
652 mListener = KSocketFactory::listen( QLatin1String(
"knotes") , QHostAddress::Any,
653 KNotesGlobalConfig::port() );
654 connect( mListener, SIGNAL(newConnection()), SLOT(slotAcceptConnection()) );
655 mPublisher=
new DNSSD::PublicService(KNotesGlobalConfig::senderID(), QLatin1String(
"_knotes._tcp"), KNotesGlobalConfig::port());
656 mPublisher->publishAsync();
660 void KNotesPart::slotAcceptConnection()
663 QTcpSocket *s = mListener->nextPendingConnection();
666 KNotesNetworkReceiver *recv =
new KNotesNetworkReceiver( s );
667 connect( recv,SIGNAL(sigNoteReceived(QString,QString)), SLOT(
newNote(QString,QString)) );
671 void KNotesPart::slotSetAlarm()
673 if (!mNotesWidget->
notesView()->currentItem())
677 QPointer<KNoteAlarmDialog> dlg =
new KNoteAlarmDialog( knoteItem->
realName(), widget() );
678 dlg->setIncidence( knoteItem->
journal() );
685 void KNotesPart::slotNewNoteFromClipboard()
687 const QString &
text = KApplication::clipboard()->text();
691 void KNotesPart::slotSaveAs()
693 if (!mNotesWidget->
notesView()->currentItem())
698 QCheckBox *convert = 0;
701 convert =
new QCheckBox( 0 );
702 convert->setText( i18n(
"Save note as plain text" ) );
704 QPointer<KFileDialog> dlg =
new KFileDialog( url, QString(), widget(), convert );
705 dlg->setOperationMode( KFileDialog::Saving );
706 dlg->setCaption( i18n(
"Save As" ) );
712 const QString fileName = dlg->selectedFile();
713 const bool htmlFormatAndSaveAsHtml = (convert && !convert->isChecked());
715 if ( fileName.isEmpty() ) {
719 QFile file( fileName );
720 if ( file.exists() &&
721 KMessageBox::warningContinueCancel( widget(),
722 i18n(
"<qt>A file named <b>%1</b> already exists.<br />"
723 "Are you sure you want to overwrite it?</qt>",
724 QFileInfo( file ).fileName() ) ) != KMessageBox::Continue ) {
728 if ( file.open( QIODevice::WriteOnly ) ) {
729 QTextStream stream( &file );
731 doc.setHtml(knoteItem->
journal()->description());
732 if ( htmlFormatAndSaveAsHtml ) {
733 stream << doc.toHtml();
735 stream << knoteItem->
realName() + QLatin1Char(
'\n');
736 stream << doc.toPlainText();
741 void KNotesPart::slotUpdateReadOnly()
743 if (!mNotesWidget->
notesView()->currentItem())
747 const bool readOnly = mReadOnly->isChecked();
749 mNoteEdit->setText(readOnly ? i18n(
"Show Note...") : i18nc(
"@action:inmenu",
"Edit..." ));
754 #include "knotes_part.moc"
void setName(const QString &id, const QString &newName)
QMap< QString, QString > notes() const
void setText(const QString &id, const QString &newText)
QString newNoteFromClipboard(const QString &name=QString())
virtual QByteArray text(quint32 serialNumber) const =0
Journal * journal() const
QStringList notesList() const
QString name(const QString &id) const
QString text(const QString &id) const
void setIconText(const QString &text)
QString newNote(const QString &name=QString(), const QString &text=QString())
void setNote(KNotesIconViewItem *item)
void killNote(const QString &id)
void popupRMB(QListWidgetItem *item, const QPoint &pos, const QPoint &globalPos)
KNotesPart(KNotesResourceManager *manager, QObject *parent=0)