1 | /* This file is part of the KDE libraries |
2 | Copyright (C) 2001,2002,2003 Carsten Pfeiffer <pfeiffer@kde.org> |
3 | Copyright (C) 2007 Kevin Ottens <ervin@kde.org> |
4 | |
5 | library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public |
7 | License as published by the Free Software Foundation, version 2. |
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 "kfileplaceeditdialog.h" |
21 | |
22 | #include <kaboutdata.h> |
23 | #include <kconfig.h> |
24 | #include <kdebug.h> |
25 | #include <kglobal.h> |
26 | #include <kicondialog.h> |
27 | #include <kiconloader.h> |
28 | #include <kcomponentdata.h> |
29 | #include <klineedit.h> |
30 | #include <klocale.h> |
31 | #include <kmimetype.h> |
32 | #include <kio/global.h> |
33 | #include <kprotocolinfo.h> |
34 | #include <kstringhandler.h> |
35 | #include <kurlrequester.h> |
36 | |
37 | #include <QtCore/QMimeData> |
38 | #include <QtGui/QApplication> |
39 | #include <QtGui/QCheckBox> |
40 | #include <QtGui/qdrawutil.h> |
41 | #include <QtGui/QFontMetrics> |
42 | #include <QtGui/QFormLayout> |
43 | #include <QtGui/QItemDelegate> |
44 | #include <QtGui/QLabel> |
45 | #include <QtGui/QMenu> |
46 | #include <QtGui/QPainter> |
47 | #include <QtGui/QStyle> |
48 | |
49 | #include <unistd.h> |
50 | #include <kvbox.h> |
51 | #include <kconfiggroup.h> |
52 | |
53 | |
54 | bool KFilePlaceEditDialog::getInformation(bool allowGlobal, KUrl& url, |
55 | QString& label, QString& icon, |
56 | bool isAddingNewPlace, |
57 | bool& appLocal, int iconSize, |
58 | QWidget *parent ) |
59 | { |
60 | KFilePlaceEditDialog *dialog = new KFilePlaceEditDialog(allowGlobal, url, |
61 | label, icon, |
62 | isAddingNewPlace, |
63 | appLocal, |
64 | iconSize, |
65 | parent ); |
66 | if ( dialog->exec() == QDialog::Accepted ) { |
67 | // set the return parameters |
68 | url = dialog->url(); |
69 | label = dialog->label(); |
70 | icon = dialog->icon(); |
71 | appLocal = dialog->applicationLocal(); |
72 | |
73 | delete dialog; |
74 | return true; |
75 | } |
76 | |
77 | delete dialog; |
78 | return false; |
79 | } |
80 | |
81 | KFilePlaceEditDialog::KFilePlaceEditDialog(bool allowGlobal, const KUrl& url, |
82 | const QString& label, |
83 | const QString &icon, |
84 | bool isAddingNewPlace, |
85 | bool appLocal, int iconSize, |
86 | QWidget *parent) |
87 | : KDialog( parent ) |
88 | { |
89 | if (isAddingNewPlace) |
90 | setCaption( i18n("Add Places Entry" ) ); |
91 | else |
92 | setCaption( i18n("Edit Places Entry" ) ); |
93 | setButtons( Ok | Cancel ); |
94 | setModal(true); |
95 | setDefaultButton(Ok); |
96 | |
97 | QWidget *wdg = new QWidget( this ); |
98 | QVBoxLayout *box = new QVBoxLayout( wdg ); |
99 | |
100 | QFormLayout *layout = new QFormLayout(); |
101 | box->addLayout( layout ); |
102 | |
103 | QString whatsThisText = i18n("<qt>This is the text that will appear in the Places panel.<br /><br />" |
104 | "The label should consist of one or two words " |
105 | "that will help you remember what this entry refers to. " |
106 | "If you do not enter a label, it will be derived from " |
107 | "the location's URL.</qt>" ); |
108 | m_labelEdit = new KLineEdit(wdg); |
109 | layout->addRow(i18n("L&abel:" ), m_labelEdit); |
110 | m_labelEdit->setText(label); |
111 | m_labelEdit->setClickMessage(i18n("Enter descriptive label here" )); |
112 | m_labelEdit->setWhatsThis(whatsThisText); |
113 | layout->labelForField(m_labelEdit)->setWhatsThis(whatsThisText); |
114 | |
115 | whatsThisText = i18n("<qt>This is the location associated with the entry. Any valid URL may be used. For example:<br /><br />" |
116 | "%1<br />http://www.kde.org<br />ftp://ftp.kde.org/pub/kde/stable<br /><br />" |
117 | "By clicking on the button next to the text edit box you can browse to an " |
118 | "appropriate URL.</qt>" , QDir::homePath()); |
119 | m_urlEdit = new KUrlRequester( url.prettyUrl(), wdg ); |
120 | m_urlEdit->setMode( KFile::Directory ); |
121 | layout->addRow( i18n("&Location:" ), m_urlEdit ); |
122 | m_urlEdit->setWhatsThis( whatsThisText ); |
123 | layout->labelForField(m_urlEdit)->setWhatsThis( whatsThisText ); |
124 | // Room for at least 40 chars (average char width is half of height) |
125 | m_urlEdit->setMinimumWidth( m_urlEdit->fontMetrics().height() * (40 / 2) ); |
126 | |
127 | whatsThisText = i18n("<qt>This is the icon that will appear in the Places panel.<br /><br />" |
128 | "Click on the button to select a different icon.</qt>" ); |
129 | m_iconButton = new KIconButton( wdg ); |
130 | layout->addRow( i18n("Choose an &icon:" ), m_iconButton ); |
131 | m_iconButton->setObjectName( QLatin1String( "icon button" ) ); |
132 | m_iconButton->setIconSize( iconSize ); |
133 | m_iconButton->setIconType( KIconLoader::NoGroup, KIconLoader::Place ); |
134 | if ( icon.isEmpty() ) |
135 | m_iconButton->setIcon( KMimeType::iconNameForUrl( url ) ); |
136 | else |
137 | m_iconButton->setIcon( icon ); |
138 | m_iconButton->setWhatsThis( whatsThisText ); |
139 | layout->labelForField(m_iconButton)->setWhatsThis( whatsThisText ); |
140 | |
141 | if ( allowGlobal ) { |
142 | QString appName; |
143 | if ( KGlobal::mainComponent().aboutData() ) |
144 | appName = KGlobal::mainComponent().aboutData()->programName(); |
145 | if ( appName.isEmpty() ) |
146 | appName = KGlobal::mainComponent().componentName(); |
147 | m_appLocal = new QCheckBox( i18n("&Only show when using this application (%1)" , appName ), wdg ); |
148 | m_appLocal->setChecked( appLocal ); |
149 | m_appLocal->setWhatsThis(i18n("<qt>Select this setting if you want this " |
150 | "entry to show only when using the current application (%1).<br /><br />" |
151 | "If this setting is not selected, the entry will be available in all " |
152 | "applications.</qt>" , |
153 | appName)); |
154 | box->addWidget(m_appLocal); |
155 | } |
156 | else |
157 | m_appLocal = 0L; |
158 | connect(m_urlEdit->lineEdit(),SIGNAL(textChanged(QString)),this,SLOT(urlChanged(QString))); |
159 | if (!label.isEmpty()) { |
160 | // editing existing entry |
161 | m_labelEdit->setFocus(); |
162 | } else { |
163 | // new entry |
164 | m_urlEdit->setFocus(); |
165 | } |
166 | setMainWidget( wdg ); |
167 | } |
168 | |
169 | KFilePlaceEditDialog::~KFilePlaceEditDialog() |
170 | { |
171 | } |
172 | |
173 | void KFilePlaceEditDialog::urlChanged(const QString & text ) |
174 | { |
175 | enableButtonOk( !text.isEmpty() ); |
176 | } |
177 | |
178 | KUrl KFilePlaceEditDialog::url() const |
179 | { |
180 | return m_urlEdit->url(); |
181 | } |
182 | |
183 | QString KFilePlaceEditDialog::label() const |
184 | { |
185 | if (!m_labelEdit->text().isEmpty()) { |
186 | return m_labelEdit->text(); |
187 | } |
188 | |
189 | // derive descriptive label from the URL |
190 | KUrl url = m_urlEdit->url(); |
191 | if (!url.fileName().isEmpty()) { |
192 | return url.fileName(); |
193 | } |
194 | if (!url.host().isEmpty()) { |
195 | return url.host(); |
196 | } |
197 | return url.scheme(); |
198 | } |
199 | |
200 | const QString &KFilePlaceEditDialog::icon() const |
201 | { |
202 | return m_iconButton->icon(); |
203 | } |
204 | |
205 | bool KFilePlaceEditDialog::applicationLocal() const |
206 | { |
207 | if ( !m_appLocal ) |
208 | return true; |
209 | |
210 | return m_appLocal->isChecked(); |
211 | } |
212 | |
213 | |
214 | #include "kfileplaceeditdialog.moc" |
215 | |