File indexing completed on 2024-05-12 16:36:08

0001 /* This file is part of the KDE project
0002    Copyright 2008 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "HyperlinkStrategy.h"
0021 
0022 #include "Selection.h"
0023 #include "Sheet.h"
0024 #include "Util.h"
0025 #include "CellToolBase.h"
0026 
0027 #include <KoCanvasBase.h>
0028 #include <KoSelection.h>
0029 #include <KoToolBase.h>
0030 
0031 #include <kmessagebox.h>
0032 #include <krun.h>
0033 
0034 #include <QMimeDatabase>
0035 #include <QTimer>
0036 #include <QUrl>
0037 
0038 using namespace Calligra::Sheets;
0039 
0040 class HyperlinkStrategy::Private
0041 {
0042 public:
0043     QPointF lastPoint;
0044     QRectF textRect;
0045     QString url;
0046 };
0047 
0048 HyperlinkStrategy::HyperlinkStrategy(CellToolBase *cellTool,
0049                                      const QPointF& documentPos, Qt::KeyboardModifiers modifiers,
0050                                      const QString& url, const QRectF& textRect)
0051         : AbstractSelectionStrategy(cellTool, documentPos, modifiers)
0052         , d(new Private)
0053 {
0054     d->lastPoint = documentPos;
0055     d->textRect = textRect;
0056     d->textRect.moveTo(d->lastPoint); // turn textRect which is relative to the cell to an absolute coordinate
0057     d->url = url;
0058 }
0059 
0060 HyperlinkStrategy::~HyperlinkStrategy()
0061 {
0062     delete d;
0063 }
0064 
0065 void HyperlinkStrategy::handleMouseMove(const QPointF& documentPos, Qt::KeyboardModifiers modifiers)
0066 {
0067     const QPointF position = documentPos - cellTool()->offset();
0068     d->lastPoint = position;
0069     if (d->textRect.contains(position))
0070         return;
0071     AbstractSelectionStrategy::handleMouseMove(documentPos, modifiers);
0072 }
0073 
0074 void HyperlinkStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)
0075 {
0076     if (!d->textRect.contains(d->lastPoint)) {
0077         return;
0078     }
0079     Q_UNUSED(modifiers)
0080     selection()->activeSheet()->showStatusMessage(i18n("Link %1 activated", d->url));
0081 
0082     const QUrl url(d->url);
0083     if (!url.isValid() || url.isRelative()) {
0084         const Region region(d->url, selection()->activeSheet()->map(), selection()->activeSheet());
0085         if (region.isValid()) {
0086             if (region.firstSheet() != selection()->activeSheet()) {
0087                 selection()->emitVisibleSheetRequested(region.firstSheet());
0088             }
0089             selection()->initialize(region);
0090 
0091             if (!region.firstRange().isNull()) {
0092                 const Cell cell = Cell(region.firstSheet(), region.firstRange().topLeft());
0093             }
0094         }
0095     } else {
0096         const QString type = QMimeDatabase().mimeTypeForUrl(url).name();
0097         if (!Util::localReferenceAnchor(d->url)) {
0098             const bool executable = KRun::isExecutableFile(url, type);
0099             if (executable) {
0100                 const QString question = i18n("This link points to the program or script '%1'.\n"
0101                                               "Malicious programs can harm your computer. "
0102                                               "Are you sure that you want to run this program?", d->url);
0103                 // this will also start local programs, so adding a "don't warn again"
0104                 // checkbox will probably be too dangerous
0105                 const int answer = KMessageBox::warningYesNo(tool()->canvas()->canvasWidget(), question,
0106                                    i18n("Open Link?"));
0107                 if (answer != KMessageBox::Yes) {
0108                     return;
0109                 }
0110             }
0111             new KRun(url, tool()->canvas()->canvasWidget(), 0);
0112         }
0113     }
0114 
0115     tool()->repaintDecorations();
0116 }