1 /* This file is part of the KDE project
2 Copyright (C) 2013 Oleg Kukharchuk <oleg.kuh@gmail.com>
3
4 This program 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 program 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 program; see the file COPYING. 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 "KexiPushButton.h"
21 #include <KLocalizedString>
22
23 #include <QDir>
24 #include <QUrl>
25
26 class Q_DECL_HIDDEN KexiPushButton::Private
27 {
28 public:
Private(KexiPushButton * qq)29 explicit Private(KexiPushButton *qq)
30 : hyperlinkType(KexiPushButton::NoHyperlink)
31 , hyperlinkTool(KexiUtils::OpenHyperlinkOptions::DefaultHyperlinkTool)
32 , executable(false)
33 , remote(false)
34 , q(qq)
35 {
36 QObject::connect(q, SIGNAL(clicked()), q, SLOT(slotClicked()));
37 }
38
39 QString hyperlink;
40 KexiPushButton::HyperlinkType hyperlinkType;
41 KexiUtils::OpenHyperlinkOptions::HyperlinkTool hyperlinkTool;
42 bool executable;
43 bool remote;
44 QString basePath;
45
46 KexiPushButton *q;
47 };
48
KexiPushButton(QWidget * parent)49 KexiPushButton::KexiPushButton(QWidget *parent)
50 : QPushButton(parent)
51 , d(new Private(this))
52 {
53 }
54
KexiPushButton(const QString & text,QWidget * parent)55 KexiPushButton::KexiPushButton(const QString &text, QWidget *parent)
56 : QPushButton(parent)
57 , d(new Private(this))
58 {
59 setText(text);
60 }
61
~KexiPushButton()62 KexiPushButton::~KexiPushButton()
63 {
64 delete d;
65 }
66
setHyperlink(const QString & url)67 void KexiPushButton::setHyperlink(const QString &url)
68 {
69 d->hyperlink = url;
70 }
71
hyperlink() const72 QString KexiPushButton::hyperlink() const
73 {
74 return d->hyperlink;
75 }
76
setHyperlinkType(HyperlinkType type)77 void KexiPushButton::setHyperlinkType(HyperlinkType type)
78 {
79 d->hyperlinkType = type;
80 }
81
hyperlinkType() const82 KexiPushButton::HyperlinkType KexiPushButton::hyperlinkType() const
83 {
84 return d->hyperlinkType;
85 }
86
setHyperlinkTool(KexiUtils::OpenHyperlinkOptions::HyperlinkTool tool)87 void KexiPushButton::setHyperlinkTool(KexiUtils::OpenHyperlinkOptions::HyperlinkTool tool)
88 {
89 d->hyperlinkTool = tool;
90 }
91
hyperlinkTool() const92 KexiUtils::OpenHyperlinkOptions::HyperlinkTool KexiPushButton::hyperlinkTool() const
93 {
94 return d->hyperlinkTool;
95 }
96
setHyperlinkExecutable(bool exec)97 void KexiPushButton::setHyperlinkExecutable(bool exec)
98 {
99 d->executable = exec;
100 }
101
isHyperlinkExecutable() const102 bool KexiPushButton::isHyperlinkExecutable() const
103 {
104 return d->executable;
105 }
106
setRemoteHyperlink(bool remote)107 void KexiPushButton::setRemoteHyperlink(bool remote)
108 {
109 d->remote = remote;
110 }
111
isRemoteHyperlink() const112 bool KexiPushButton::isRemoteHyperlink() const
113 {
114 return d->remote;
115 }
116
setLocalBasePath(const QString & basePath)117 void KexiPushButton::setLocalBasePath(const QString &basePath)
118 {
119 d->basePath = basePath;
120 }
121
slotClicked()122 void KexiPushButton::slotClicked()
123 {
124 if (d->hyperlinkType == KexiPushButton::NoHyperlink) {
125 return;
126 }
127
128 QUrl url(d->hyperlink);
129 if (d->hyperlinkTool == KexiUtils::OpenHyperlinkOptions::MailerHyperlinkTool
130 && url.scheme().isEmpty())
131 {
132 url.setScheme("mailto");
133 }
134
135 if (url.isRelative()) {
136 url.setUrl(d->basePath + QDir::separator() + d->hyperlink);
137 url.setScheme("file");
138 }
139
140 KexiUtils::OpenHyperlinkOptions opt;
141 opt.allowExecutable = d->executable;
142 opt.allowRemote = d->remote;
143 opt.tool = d->hyperlinkTool;
144 KexiUtils::openHyperLink(url, this, opt);
145 }
146