1/* This file is part of the KDE project
2 Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
3 Copyright (C) 2002 Joseph Wenninger <jowenn@kde.org>
4 Copyright (C) 2002 Anders Lund <anders.lund@lund.tdcadsl.dk>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License version 2 as published by the Free Software Foundation.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "katemailfiles.h"
22#include "katemailfiles.moc"
23
24#include "katemailfilesdialog.h"
25
26#include <kicon.h>
27#include <kiconloader.h>
28#include <ktexteditor/document.h>
29#include <ktexteditor/view.h>
30
31#include <kparts/part.h>
32#include <kaction.h>
33#include <kactioncollection.h>
34
35#include <kurl.h>
36#include <klibloader.h>
37#include <klocale.h>
38#include <kdebug.h>
39#include <kmessagebox.h>
40#include <KToolInvocation>
41
42#include <kpluginfactory.h>
43#include <kpluginloader.h>
44#include <kaboutdata.h>
45#include <kauthorized.h>
46
47K_PLUGIN_FACTORY(KateMailFilesFactory, registerPlugin<KateMailFilesPlugin>();)
48K_EXPORT_PLUGIN(KateMailFilesFactory(KAboutData("katemailfilesplugin","katemailfilesplugin",ki18n("Mail Files"), "0.1", ki18n("Support mailing files"), KAboutData::License_LGPL_V2)) )
49
50KateMailFilesPlugin::KateMailFilesPlugin( QObject* parent, const QList<QVariant>& ):
51 Kate::Plugin ( (Kate::Application*)parent )
52{}
53
54Kate::PluginView *KateMailFilesPlugin::createView (Kate::MainWindow *mainWindow)
55{
56 return new KateMailFilesPluginView (mainWindow);
57}
58
59KateMailFilesPluginView::KateMailFilesPluginView (Kate::MainWindow *mainWindow)
60 : Kate::PluginView (mainWindow), Kate::XMLGUIClient(KateMailFilesFactory::componentData())
61{
62 actionCollection()->addAction( KStandardAction::Mail, this, SLOT(slotMail()) )
63 ->setWhatsThis(i18n("Send one or more of the open documents as email attachments."));
64 mainWindow->guiFactory()->addClient (this);
65}
66
67KateMailFilesPluginView::~KateMailFilesPluginView ()
68{
69 mainWindow()->guiFactory()->removeClient (this);
70}
71
72void KateMailFilesPluginView::slotMail()
73{
74 KateMailDialog *d = new KateMailDialog(mainWindow()->window(), mainWindow());
75 if ( ! d->exec() )
76 {
77 delete d;
78 return;
79 }
80 QList<KTextEditor::Document *> attDocs = d->selectedDocs();
81 delete d;
82 // Check that all selected files are saved (or shouldn't be)
83 QStringList urls; // to atthatch
84 KTextEditor::Document *doc;
85 for ( QList<KTextEditor::Document *>::iterator it = attDocs.begin();
86 it != attDocs.end(); ++it )
87 {
88 doc = *it;
89 if (!doc) continue;
90 if ( doc->url().isEmpty() )
91 {
92 // unsaved document. back out unless it gets saved
93 int r = KMessageBox::questionYesNo( mainWindow()->window(),
94 i18n("<p>The current document has not been saved, and "
95 "cannot be attached to an email message.</p>"
96 "<p>Do you want to save it and proceed?</p>"),
97 i18n("Cannot Send Unsaved File"), KStandardGuiItem::saveAs(), KStandardGuiItem::cancel() );
98 if ( r == KMessageBox::Yes )
99 {
100 bool sr = doc->documentSaveAs();
101 /* if ( sr == KTextEditor::View::SAVE_OK ) { ;
102 }
103 else {*/
104 if ( !sr ) // ERROR or RETRY(?)
105 { KMessageBox::sorry( mainWindow()->window(), i18n("The file could not be saved. Please check "
106 "if you have write permission.") );
107 continue;
108 }
109 }
110 else
111 continue;
112 }
113 if ( doc->isModified() )
114 {
115 // warn that document is modified and offer to save it before proceeding.
116 int r = KMessageBox::warningYesNoCancel( mainWindow()->window(),
117 i18n("<p>The current file:<br /><strong>%1</strong><br />has been "
118 "modified. Modifications will not be available in the attachment.</p>"
119 "<p>Do you want to save it before sending it?</p>", doc->url().pathOrUrl()),
120 i18n("Save Before Sending?"), KStandardGuiItem::save(), KGuiItem(i18n("Do Not Save")) );
121 switch ( r )
122 {
123 case KMessageBox::Cancel:
124 continue;
125 case KMessageBox::Yes:
126 doc->save();
127 if ( doc->isModified() )
128 { // read-only docs ends here, if modified. Hmm.
129 KMessageBox::sorry( mainWindow()->window(), i18n("The file could not be saved. Please check "
130 "if you have write permission.") );
131 continue;
132 }
133 break;
134 default:
135 break;
136 }
137 }
138 // finally call the mailer
139 urls << doc->url().url();
140 } // check selected docs done
141 if ( ! urls.count() )
142 return;
143 KToolInvocation::invokeMailer( QString(), // to
144 QString(), // cc
145 QString(), // bcc
146 QString(), // subject
147 QString(), // body
148 QString(), // msgfile
149 urls // urls to atthatch
150 );
151}
152// kate: space-indent on; indent-width 2; replace-tabs on;
153
154