36 #include <QStyleOption>
37 #include <QVBoxLayout>
41 using namespace KParts;
44 class KParts::BrowserOpenOrSaveQuestionPrivate : public
KDialog
49 static const KDialog::ButtonCode
Save = KDialog::Yes;
50 static const KDialog::ButtonCode OpenDefault = KDialog::User2;
51 static const KDialog::ButtonCode OpenWith = KDialog::User1;
52 static const KDialog::ButtonCode
Cancel = KDialog::Cancel;
54 BrowserOpenOrSaveQuestionPrivate(
QWidget* parent,
const KUrl& url,
const QString& mimeType)
55 : KDialog(parent), url(url), mimeType(mimeType),
61 setCaption(url.host());
62 setButtons(Save | OpenDefault | OpenWith | Cancel);
63 setObjectName(
"questionYesNoCancel");
66 setDefaultButton(Save);
68 QVBoxLayout *mainLayout =
new QVBoxLayout(mainWidget());
69 mainLayout->setSpacing(KDialog::spacingHint() * 2);
70 mainLayout->setMargin(0);
72 QHBoxLayout *hLayout =
new QHBoxLayout();
73 hLayout->setMargin(0);
74 hLayout->setSpacing(-1);
75 mainLayout->addLayout(hLayout, 5);
79 option.initFrom(
this);
80 KIcon icon(
"dialog-information");
81 iconLabel->setPixmap(icon.pixmap(style()->pixelMetric(QStyle::PM_MessageBoxIconSize, &option,
this)));
83 hLayout->addWidget(iconLabel, 0, Qt::AlignCenter);
84 hLayout->addSpacing(KDialog::spacingHint());
86 QVBoxLayout* textVLayout =
new QVBoxLayout;
88 textVLayout->addWidget(questionLabel);
90 fileNameLabel =
new QLabel(mainWidget());
91 fileNameLabel->hide();
92 textVLayout->addWidget(fileNameLabel);
94 mime = KMimeType::mimeType(mimeType, KMimeType::ResolveAliases);
95 QString mimeDescription (mimeType);
98 mimeDescription = (mime->comment().isEmpty() ? mime->name() : mime->comment());
101 mimeTypeLabel->setText(
i18nc(
"@label Type of file",
"Type: %1", mimeDescription));
102 mimeTypeLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
103 textVLayout->addWidget(mimeTypeLabel);
105 hLayout->addLayout(textVLayout,5);
107 mainLayout->addStretch(15);
108 dontAskAgainCheckBox =
new QCheckBox(mainWidget());
109 dontAskAgainCheckBox->setText(
i18nc(
"@label:checkbox",
"Remember action for files of this type"));
110 mainLayout->addWidget(dontAskAgainCheckBox);
113 bool autoEmbedMimeType(
int flags);
115 int executeDialog(
const QString& dontShowAgainName)
117 KConfigGroup cg(dontAskConfig,
"Notification Messages");
118 const QString dontAsk = cg.readEntry(dontShowAgainName,
QString()).toLower();
119 if (dontAsk ==
"yes" || dontAsk ==
"true") {
121 }
else if (dontAsk ==
"no" || dontAsk ==
"false") {
126 questionLabel->text(),
129 const int result = exec();
131 if (dontAskAgainCheckBox->isChecked()) {
132 cg.writeEntry(dontShowAgainName, result == Save);
140 KGuiItem openItem(
i18nc(
"@label:button",
"&Open with %1", selectedService->name()), selectedService->icon());
141 setButtonGuiItem(OpenWith, openItem);
149 BrowserOpenOrSaveQuestion::Features features;
153 virtual void slotButtonClicked(
int buttonId)
155 if (buttonId != OpenDefault)
157 KPushButton* button = KDialog::button(KDialog::ButtonCode(buttonId));
158 if (button && !button->menu()) {
163 QCheckBox* dontAskAgainCheckBox;
167 void slotAppSelected(
QAction* action)
177 : d(new BrowserOpenOrSaveQuestionPrivate(parent, url, mimeType))
188 QString actionName(service->name().replace(
'&',
"&&"));
189 actionName =
i18nc(
"@action:inmenu",
"Open &with %1", actionName);
192 act->setIcon(
KIcon(service->icon()));
193 act->setText(actionName);
194 act->setData(QVariant::fromValue(service));
200 d->questionLabel->setText(
i18nc(
"@info",
"Open '%1'?", d->url.pathOrUrl()));
201 d->questionLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
202 d->showButton(BrowserOpenOrSaveQuestionPrivate::OpenWith,
false);
204 KGuiItem openWithDialogItem(
i18nc(
"@label:button",
"&Open with..."),
"document-open");
211 if (apps.isEmpty()) {
212 d->setButtonGuiItem(BrowserOpenOrSaveQuestionPrivate::OpenDefault, openWithDialogItem);
215 KGuiItem openItem(
i18nc(
"@label:button",
"&Open with %1", offer->name()), offer->
icon());
216 d->setButtonGuiItem(BrowserOpenOrSaveQuestionPrivate::OpenDefault, openItem);
219 d->selectedService = apps.first();
220 d->showButton(BrowserOpenOrSaveQuestionPrivate::OpenWith,
true);
222 if (apps.count() > 1) {
224 KGuiItem openWithItem(
i18nc(
"@label:button",
"&Open with"),
"document-open");
225 d->setButtonGuiItem(BrowserOpenOrSaveQuestionPrivate::OpenWith, openWithItem);
226 d->setButtonMenu(BrowserOpenOrSaveQuestionPrivate::OpenWith, menu, KDialog::InstantPopup);
227 QObject::connect(menu, SIGNAL(triggered(
QAction*)), d, SLOT(slotAppSelected(
QAction*)));
228 for (KService::List::const_iterator it = apps.begin(); it != apps.end(); ++it) {
230 menu->addAction(act);
233 openWithDialogAction->setIcon(
KIcon(
"document-open"));
234 openWithDialogAction->setText(openWithDialogItem.
text());
235 menu->addAction(openWithDialogAction);
238 d->setButtonGuiItem(BrowserOpenOrSaveQuestionPrivate::OpenWith, openWithDialogItem);
241 kDebug() <<
"Not using new feature ServiceSelection; port the caller to BrowserOpenOrSaveQuestion::setFeature(ServiceSelection)";
247 const QString dontAskAgain = QLatin1String(
"askSave") + d->mimeType;
249 const int choice = d->executeDialog(dontAskAgain);
250 return choice == BrowserOpenOrSaveQuestionPrivate::Save ? Save
251 : (choice == BrowserOpenOrSaveQuestionPrivate::Cancel ? Cancel :
Open);
256 return d->selectedService;
259 bool BrowserOpenOrSaveQuestionPrivate::autoEmbedMimeType(
int flags)
275 mime->is(
"text/html") ||
276 mime->is(
"application/xml") ||
277 mime->is(
"inode/directory") ||
278 mimeType.startsWith(QLatin1String(
"image")) ||
279 mime->is(
"multipart/x-mixed-replace") ||
280 mime->is(
"multipart/replace")))
287 if (d->autoEmbedMimeType(flags))
291 d->setButtonGuiItem(BrowserOpenOrSaveQuestionPrivate::OpenDefault,
KGuiItem(
i18nc(
"@label:button",
"&Open"),
"document-open"));
292 d->showButton(BrowserOpenOrSaveQuestionPrivate::OpenWith,
false);
294 d->questionLabel->setText(
i18nc(
"@info",
"Open '%1'?", d->url.pathOrUrl()));
295 d->questionLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
297 const QString dontAskAgain = QLatin1String(
"askEmbedOrSave")+ d->mimeType;
300 const int choice = d->executeDialog(dontAskAgain);
301 return choice == BrowserOpenOrSaveQuestionPrivate::Save ? Save
302 : (choice == BrowserOpenOrSaveQuestionPrivate::Cancel ? Cancel :
Embed);
307 d->features = features;
312 if (suggestedFileName.isEmpty()) {
316 d->fileNameLabel->setText(
i18nc(
"@label File name",
"Name: %1", suggestedFileName));
317 d->fileNameLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
318 d->fileNameLabel->setWhatsThis(
i18nc(
"@info:whatsthis",
"This is the file name suggested by the server"));
319 d->fileNameLabel->show();
322 #include "browseropenorsavequestion.moc"
void setSuggestedFileName(const QString &suggestedFileName)
Sets the suggested filename, shown in the dialog.
static KAction * createAppAction(const KService::Ptr &service, QObject *parent)
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
QString i18nc(const char *ctxt, const char *text)
void setFeatures(Features features)
Enables the given features in the dialog.
Result askOpenOrSave()
Ask the user whether to save or open a url in another application.
~BrowserOpenOrSaveQuestion()
BrowserOpenOrSaveQuestion(QWidget *parent, const KUrl &url, const QString &mimeType)
Constructor, for all kinds of dialogs shown in this class.
static KNotification * event(const QString &eventId, const QString &title, const QString &text, const QPixmap &pixmap=QPixmap(), QWidget *widget=0L, const NotificationFlags &flags=CloseOnTimeout, const KComponentData &componentData=KComponentData())
Result askEmbedOrSave(int flags=0)
Ask the user whether to save or open a url in another application.
static KService::List associatedApplications(const QStringList &mimeTypeList, const QString &traderConstraint)
KService::Ptr selectedService() const
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, const char *resourceType="config")
Shows "Open With..." with the associated applications for the mimetype.