Move in own directory
[kdepim.git] / blogilo / src / composer / webkit / blogilocomposereditor.cpp
blob62c7cdabd1624fc13dfd1884c670100297be019c
1 /*
2 Copyright (c) 2013-2016 Montel Laurent <montel@kde.org>
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
21 #include "blogilocomposereditor.h"
23 #include "bilbomedia.h"
24 #include "global.h"
26 #include "blogilo_debug.h"
28 #include <QAction>
29 #include <KLocalizedString>
30 #include <QIcon>
32 #include <QWebElementCollection>
33 #include <QWebFrame>
34 #include <QMimeDatabase>
35 #include <QMimeType>
37 BlogiloComposerEditor::BlogiloComposerEditor(BlogiloComposerView *view, QWidget *parent)
38 : ComposerEditorNG::ComposerEditor(view, parent),
39 readOnly(false)
41 QList<ComposerEditorNG::ComposerView::ComposerViewAction> lstActions;
42 lstActions << ComposerEditorNG::ComposerView::Separator;
43 lstActions << ComposerEditorNG::ComposerView::Bold;
44 lstActions << ComposerEditorNG::ComposerView::Italic;
45 lstActions << ComposerEditorNG::ComposerView::Underline;
46 lstActions << ComposerEditorNG::ComposerView::StrikeOut;
47 lstActions << ComposerEditorNG::ComposerView::Separator;
48 lstActions << ComposerEditorNG::ComposerView::FormatType;
49 lstActions << ComposerEditorNG::ComposerView::FontSize;
50 lstActions << ComposerEditorNG::ComposerView::TextForegroundColor;
51 lstActions << ComposerEditorNG::ComposerView::FormatReset;
52 lstActions << ComposerEditorNG::ComposerView::BlockQuote;
53 lstActions << ComposerEditorNG::ComposerView::Separator;
54 lstActions << ComposerEditorNG::ComposerView::InsertLink;
55 lstActions << ComposerEditorNG::ComposerView::InsertImage;
56 lstActions << ComposerEditorNG::ComposerView::Separator;
57 lstActions << ComposerEditorNG::ComposerView::AlignLeft;
58 lstActions << ComposerEditorNG::ComposerView::AlignCenter;
59 lstActions << ComposerEditorNG::ComposerView::AlignRight;
60 lstActions << ComposerEditorNG::ComposerView::AlignJustify;
61 lstActions << ComposerEditorNG::ComposerView::Separator;
62 lstActions << ComposerEditorNG::ComposerView::DirectionRtl;
63 lstActions << ComposerEditorNG::ComposerView::OrderedList;
64 lstActions << ComposerEditorNG::ComposerView::UnorderedList;
65 lstActions << ComposerEditorNG::ComposerView::Separator;
66 lstActions << ComposerEditorNG::ComposerView::InsertTable;
68 QList<ComposerEditorNG::ComposerView::ComposerViewAction> toolBarActions;
69 toolBarActions << lstActions;
71 QList<ComposerEditorNG::ComposerView::ComposerViewAction> lstActionsFormat;
72 lstActionsFormat << ComposerEditorNG::ComposerView::SubScript;
73 lstActionsFormat << ComposerEditorNG::ComposerView::SuperScript;
74 lstActionsFormat << ComposerEditorNG::ComposerView::Separator;
75 lstActionsFormat << ComposerEditorNG::ComposerView::ListIndent;
76 lstActionsFormat << ComposerEditorNG::ComposerView::ListDedent;
77 lstActionsFormat << ComposerEditorNG::ComposerView::Separator;
78 lstActionsFormat << ComposerEditorNG::ComposerView::TextBackgroundColor;
80 //Create all actions first (before to add to toolbar)
81 createActions(lstActions << ComposerEditorNG::ComposerView::PasteWithoutFormatting << lstActionsFormat);
83 KToolBar *mainToolBar = createToolBar(toolBarActions);
85 mActSplitPost = new QAction(QIcon::fromTheme(QStringLiteral("insert-more-mark")), i18n("Split text"), this);
86 connect(mActSplitPost, &QAction::triggered, this, &BlogiloComposerEditor::slotAddPostSplitter);
87 addActionInToolBar(mActSplitPost, mainToolBar);
89 mActCode = new QAction(QIcon::fromTheme(QStringLiteral("format-text-code")), i18nc("Sets text font to code style",
90 "Code"), this);
91 connect(mActCode, &QAction::triggered, this, &BlogiloComposerEditor::slotToggleCode);
92 addActionInToolBar(mActCode, mainToolBar);
94 createToolBar(lstActionsFormat);
97 BlogiloComposerEditor::~BlogiloComposerEditor()
102 void BlogiloComposerEditor::setReadOnly(bool _readOnly)
104 if (readOnly != _readOnly) {
105 readOnly = _readOnly;
106 view()->evaluateJavascript(QStringLiteral("setReadOnly(%1)").arg(readOnly ? QStringLiteral("true") : QStringLiteral("false")));
110 QList< BilboMedia * > BlogiloComposerEditor::getLocalImages()
112 qCDebug(BLOGILO_LOG);
113 QList< BilboMedia * > list;
114 QWebElementCollection images = view()->page()->mainFrame()->findAllElements(QStringLiteral("img"));
115 Q_FOREACH (const QWebElement &elm, images) {
116 if (elm.attribute(QStringLiteral("src")).startsWith(QStringLiteral("file://"))) {
117 // qCDebug(BLOGILO_LOG)<<elm.toOuterXml();
118 BilboMedia *media = new BilboMedia(this);
119 QUrl mediaUrl(elm.attribute(QStringLiteral("src")));
120 media->setLocalUrl(mediaUrl);
121 QMimeDatabase db;
122 media->setMimeType(db.mimeTypeForFile(mediaUrl.path(), QMimeDatabase::MatchExtension).name());
123 media->setName(mediaUrl.fileName());
124 media->setBlogId(__currentBlogId);
125 list.append(media);
128 return list;
131 void BlogiloComposerEditor::replaceImageSrc(const QString &src, const QString &dest)
133 const QString cmd = QStringLiteral("replaceImageSrc('%1','%2')").arg(src).arg(dest);
134 view()->evaluateJavascript(cmd);
137 void BlogiloComposerEditor::slotAddPostSplitter()
139 execCommand(QStringLiteral("insertHTML"), QStringLiteral("<hr/><!--split-->"));
142 void BlogiloComposerEditor::slotToggleCode(bool)
144 const QString selection = view()->selectedText();
145 if (selection.isEmpty()) {
146 return;
148 const QString html = QStringLiteral("<code>%1</code>").arg(selection);
149 execCommand(QStringLiteral("insertHtml"), html);
152 void BlogiloComposerEditor::startEditing()
154 static_cast<BlogiloComposerView *>(view())->startEditing();
157 void BlogiloComposerEditor::execCommand(const QString &cmd)
159 QWebFrame *frame = view()->page()->mainFrame();
160 const QString js = QStringLiteral("document.execCommand(\"%1\", false, null)").arg(cmd);
161 frame->evaluateJavaScript(js);
164 void BlogiloComposerEditor::execCommand(const QString &cmd, const QString &arg)
166 QWebFrame *frame = view()->page()->mainFrame();
167 const QString js = QStringLiteral("document.execCommand(\"%1\", false, \"%2\")").arg(cmd).arg(arg);
168 frame->evaluateJavaScript(js);
171 void BlogiloComposerEditor::insertShortUrl(const QString &url)
173 QString html = QStringLiteral("<a href=\'%1\'>%1</a>").arg(url);
174 execCommand(QStringLiteral("insertHtml"), html);