1 /* This file is part of the KDE project
2    Copyright 2008 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13 
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
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17    Boston, MA 02110-1301, USA.
18 */
19 
20 #include "HyperlinkStrategy.h"
21 
22 #include "Selection.h"
23 #include "Sheet.h"
24 #include "Util.h"
25 #include "CellToolBase.h"
26 
27 #include <KoCanvasBase.h>
28 #include <KoSelection.h>
29 #include <KoToolBase.h>
30 
31 #include <kmessagebox.h>
32 #include <krun.h>
33 
34 #include <QMimeDatabase>
35 #include <QTimer>
36 #include <QUrl>
37 
38 using namespace Calligra::Sheets;
39 
40 class HyperlinkStrategy::Private
41 {
42 public:
43     QPointF lastPoint;
44     QRectF textRect;
45     QString url;
46 };
47 
HyperlinkStrategy(CellToolBase * cellTool,const QPointF & documentPos,Qt::KeyboardModifiers modifiers,const QString & url,const QRectF & textRect)48 HyperlinkStrategy::HyperlinkStrategy(CellToolBase *cellTool,
49                                      const QPointF& documentPos, Qt::KeyboardModifiers modifiers,
50                                      const QString& url, const QRectF& textRect)
51         : AbstractSelectionStrategy(cellTool, documentPos, modifiers)
52         , d(new Private)
53 {
54     d->lastPoint = documentPos;
55     d->textRect = textRect;
56     d->textRect.moveTo(d->lastPoint); // turn textRect which is relative to the cell to an absolute coordinate
57     d->url = url;
58 }
59 
~HyperlinkStrategy()60 HyperlinkStrategy::~HyperlinkStrategy()
61 {
62     delete d;
63 }
64 
handleMouseMove(const QPointF & documentPos,Qt::KeyboardModifiers modifiers)65 void HyperlinkStrategy::handleMouseMove(const QPointF& documentPos, Qt::KeyboardModifiers modifiers)
66 {
67     const QPointF position = documentPos - cellTool()->offset();
68     d->lastPoint = position;
69     if (d->textRect.contains(position))
70         return;
71     AbstractSelectionStrategy::handleMouseMove(documentPos, modifiers);
72 }
73 
finishInteraction(Qt::KeyboardModifiers modifiers)74 void HyperlinkStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)
75 {
76     if (!d->textRect.contains(d->lastPoint)) {
77         return;
78     }
79     Q_UNUSED(modifiers)
80     selection()->activeSheet()->showStatusMessage(i18n("Link %1 activated", d->url));
81 
82     const QUrl url(d->url);
83     if (!url.isValid() || url.isRelative()) {
84         const Region region(d->url, selection()->activeSheet()->map(), selection()->activeSheet());
85         if (region.isValid()) {
86             if (region.firstSheet() != selection()->activeSheet()) {
87                 selection()->emitVisibleSheetRequested(region.firstSheet());
88             }
89             selection()->initialize(region);
90 
91             if (!region.firstRange().isNull()) {
92                 const Cell cell = Cell(region.firstSheet(), region.firstRange().topLeft());
93             }
94         }
95     } else {
96         const QString type = QMimeDatabase().mimeTypeForUrl(url).name();
97         if (!Util::localReferenceAnchor(d->url)) {
98             const bool executable = KRun::isExecutableFile(url, type);
99             if (executable) {
100                 const QString question = i18n("This link points to the program or script '%1'.\n"
101                                               "Malicious programs can harm your computer. "
102                                               "Are you sure that you want to run this program?", d->url);
103                 // this will also start local programs, so adding a "don't warn again"
104                 // checkbox will probably be too dangerous
105                 const int answer = KMessageBox::warningYesNo(tool()->canvas()->canvasWidget(), question,
106                                    i18n("Open Link?"));
107                 if (answer != KMessageBox::Yes) {
108                     return;
109                 }
110             }
111             new KRun(url, tool()->canvas()->canvasWidget(), 0);
112         }
113     }
114 
115     tool()->repaintDecorations();
116 }
117