Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / konqueror / settings / konqhtml / generalopts.cpp
blob630aa8fd032775300ac34f097408699d8049c2a5
1 /*
2 * Add here all general options - those that apply to both web browsing and filemanagement mode
4 * Copyright (c) Sven Radej 1998
5 * Copyright (c) David Faure 1998
6 * Copyright (c) 2001 Waldo Bastian <bastian@kde.org>
7 * Copyright (c) 2007 Nick Shaforostoff <shafff@ukr.net>
9 */
11 // Own
12 #include "generalopts.h"
14 // Qt
15 #include <QtDBus/QDBusConnection>
16 #include <QtDBus/QDBusMessage>
17 #include <QtGui/QGroupBox>
18 #include <QtGui/QLayout>
19 #include <QtGui/QVBoxLayout>
20 #include <QtGui/QLabel>
22 // KDE
23 #include <kcombobox.h>
24 #include <kconfig.h>
25 #include <kdebug.h>
26 #include <kmimetype.h>
27 #include <kstandarddirs.h>
28 #include <kurlrequester.h>
30 // Local
31 #include "ui_advancedTabOptions.h"
32 #include "khtml_settings.h"
33 #include <KPluginFactory>
34 #include <KPluginLoader>
36 K_PLUGIN_FACTORY_DECLARATION(KcmKonqHtmlFactory)
38 // Keep in sync with konqueror.kcfg
39 static const char* DEFAULT_HOMEPAGE = "http://www.kde.org";
40 enum StartPage { ShowHomePage, ShowBlankPage, ShowAboutPage, ShowBookmarksPage };
42 //-----------------------------------------------------------------------------
44 KKonqGeneralOptions::KKonqGeneralOptions(QWidget *parent, const QVariantList&)
45 : KCModule( KcmKonqHtmlFactory::componentData(), parent )
47 m_pConfig = KSharedConfig::openConfig("konquerorrc", KConfig::NoGlobals);
48 QVBoxLayout *lay = new QVBoxLayout(this);
49 lay->setMargin(0);
50 lay->setSpacing(KDialog::spacingHint());
52 addHomeUrlWidgets(lay);
54 QGroupBox* tabsGroup = new QGroupBox(i18n("Tabbed Browsing"));
56 tabOptions = new Ui_advancedTabOptions;
57 tabOptions->setupUi(tabsGroup);
59 connect(tabOptions->m_pShowMMBInTabs, SIGNAL(toggled(bool)), SLOT(slotChanged()));
60 connect(tabOptions->m_pDynamicTabbarHide, SIGNAL(toggled(bool)), SLOT(slotChanged()));
61 connect(tabOptions->m_pNewTabsInBackground, SIGNAL(toggled(bool)), SLOT(slotChanged()));
62 connect(tabOptions->m_pOpenAfterCurrentPage, SIGNAL(toggled(bool)), SLOT(slotChanged()));
63 connect(tabOptions->m_pTabConfirm, SIGNAL(toggled(bool)), SLOT(slotChanged()));
64 connect(tabOptions->m_pTabCloseActivatePrevious, SIGNAL(toggled(bool)), SLOT(slotChanged()));
65 connect(tabOptions->m_pPermanentCloseButton, SIGNAL(toggled(bool)), SLOT(slotChanged()));
66 connect(tabOptions->m_pKonquerorTabforExternalURL, SIGNAL(toggled(bool)), SLOT(slotChanged()));
67 connect(tabOptions->m_pPopupsWithinTabs, SIGNAL(toggled(bool)), SLOT(slotChanged()));
68 connect(tabOptions->m_pMiddleClickClose, SIGNAL(toggled(bool)), SLOT(slotChanged()));
70 lay->addWidget(tabsGroup);
72 emit changed(false);
75 void KKonqGeneralOptions::addHomeUrlWidgets(QVBoxLayout* lay)
77 QHBoxLayout *startLayout = new QHBoxLayout;
78 lay->addLayout(startLayout);
80 QLabel* startLabel = new QLabel(i18nc("@label:listbox", "When &Konqueror starts:"), this);
81 startLayout->addWidget(startLabel);
83 m_startCombo = new KComboBox(this);
84 m_startCombo->setEditable(false);
85 m_startCombo->addItem(i18nc("@item:inlistbox", "Show Introduction Page"), ShowAboutPage);
86 m_startCombo->addItem(i18nc("@item:inlistbox", "Show My Home Page"), ShowHomePage);
87 m_startCombo->addItem(i18nc("@item:inlistbox", "Show Blank Page"), ShowBlankPage);
88 m_startCombo->addItem(i18nc("@item:inlistbox", "Show My Bookmarks"), ShowBookmarksPage);
89 startLayout->addWidget(m_startCombo);
90 connect(m_startCombo, SIGNAL(currentIndexChanged(int)), SLOT(slotChanged()));
92 startLabel->setBuddy(m_startCombo);
94 ////
96 QHBoxLayout *homeLayout = new QHBoxLayout;
97 QLabel *label = new QLabel(i18n("Home page:"), this);
98 homeLayout->addWidget(label);
100 homeURL = new KUrlRequester(this);
101 homeURL->setMode(KFile::Directory);
102 homeURL->setWindowTitle(i18n("Select Home Page"));
103 homeLayout->addWidget(homeURL);
104 connect(homeURL, SIGNAL(textChanged(QString)), SLOT(slotChanged()));
105 label->setBuddy(homeURL);
107 lay->addLayout(homeLayout);
109 QString homestr = i18n("This is the URL of the web page where "
110 "Konqueror (as web browser) will jump to when "
111 "the \"Home\" button is pressed. When Konqueror is "
112 "started as a file manager, that button makes it jump "
113 "to your local home folder instead.");
114 label->setWhatsThis(homestr);
115 homeURL->setWhatsThis(homestr);
118 KKonqGeneralOptions::~KKonqGeneralOptions()
120 delete tabOptions;
123 static QString readStartUrlFromProfile()
125 const QString blank = "about:blank";
126 const QString profile = KStandardDirs::locate("data", QLatin1String("konqueror/profiles/webbrowsing"));
127 if (profile.isEmpty())
128 return blank;
129 KConfig cfg(profile, KConfig::SimpleConfig);
130 KConfigGroup profileGroup(&cfg, "Profile");
131 const QString rootItem = profileGroup.readEntry("RootItem");
132 if (rootItem.isEmpty())
133 return blank;
134 if (rootItem.startsWith("View")) {
135 const QString prefix = rootItem + '_';
136 const QString urlKey = QString("URL").prepend(prefix);
137 return profileGroup.readPathEntry(urlKey, blank);
139 // simplify the other cases: whether root is a splitter or directly the tabwidget,
140 // we want to look at the first view inside the tabs, i.e. ViewT0.
141 return profileGroup.readPathEntry("ViewT0_URL", blank);
144 static StartPage urlToStartPageEnum(const QString& startUrl)
146 if (startUrl == "about:blank")
147 return ShowBlankPage;
148 if (startUrl == "about:" || startUrl == "about:konqueror")
149 return ShowAboutPage;
150 if (startUrl == "bookmarks:" || startUrl == "bookmarks:/")
151 return ShowBookmarksPage;
152 return ShowHomePage;
155 void KKonqGeneralOptions::load()
157 KConfigGroup userSettings(m_pConfig, "UserSettings");
158 homeURL->setUrl(userSettings.readEntry("HomeURL", DEFAULT_HOMEPAGE));
159 const QString startUrl = readStartUrlFromProfile();
160 const StartPage startPage = urlToStartPageEnum(startUrl);
161 const int startComboIndex = m_startCombo->findData(startPage);
162 Q_ASSERT(startComboIndex != -1);
163 m_startCombo->setCurrentIndex(startComboIndex);
165 KConfigGroup cg(m_pConfig, "FMSettings"); // ### what a wrong group name for these settings...
167 tabOptions->m_pShowMMBInTabs->setChecked( cg.readEntry( "MMBOpensTab", true ) );
168 tabOptions->m_pDynamicTabbarHide->setChecked( ! (cg.readEntry( "AlwaysTabbedMode", false )) );
170 tabOptions->m_pNewTabsInBackground->setChecked( ! (cg.readEntry( "NewTabsInFront", false)) );
171 tabOptions->m_pOpenAfterCurrentPage->setChecked( cg.readEntry( "OpenAfterCurrentPage", false) );
172 tabOptions->m_pPermanentCloseButton->setChecked( cg.readEntry( "PermanentCloseButton", false) );
173 tabOptions->m_pKonquerorTabforExternalURL->setChecked( cg.readEntry( "KonquerorTabforExternalURL", false) );
174 tabOptions->m_pPopupsWithinTabs->setChecked( cg.readEntry( "PopupsWithinTabs", false) );
175 tabOptions->m_pTabCloseActivatePrevious->setChecked( cg.readEntry( "TabCloseActivatePrevious", false) );
176 tabOptions->m_pMiddleClickClose->setChecked( cg.readEntry( "MouseMiddleClickClosesTab", false ) );
178 cg = KConfigGroup(m_pConfig, "Notification Messages");
179 tabOptions->m_pTabConfirm->setChecked( !cg.hasKey("MultipleTabConfirm") );
183 void KKonqGeneralOptions::defaults()
185 homeURL->setUrl(KUrl(DEFAULT_HOMEPAGE));
187 bool old = m_pConfig->readDefaults();
188 m_pConfig->setReadDefaults(true);
189 load();
190 m_pConfig->setReadDefaults(old);
193 static void updateWebbrowsingProfile(const QString& homeUrl, StartPage startPage)
195 QString url;
196 QString serviceType;
197 QString serviceName;
198 switch(startPage) {
199 case ShowHomePage:
201 url = homeUrl;
202 // Check if we can determine the mimetype of that URL; profile loading requires the mimetype to be known
203 // This handles the case of a local directory, at least.
204 KMimeType::Ptr mime = KMimeType::findByUrl(url);
205 if (mime && !mime->isDefault()) {
206 serviceType = mime->name();
207 } else {
208 serviceType = "text/html";
209 serviceName = "khtml";
212 break;
213 case ShowAboutPage:
214 url = "about:";
215 serviceType = "KonqAboutPage";
216 serviceName = "konq_aboutpage";
217 break;
218 case ShowBlankPage:
219 url = "about:blank";
220 serviceType = "text/html";
221 serviceName = "khtml";
222 break;
223 case ShowBookmarksPage:
224 url = "bookmarks:";
225 serviceType = "text/html";
226 serviceName = "khtml";
227 break;
230 const QString profileFileName = "webbrowsing";
232 // Create local copy of the profile if needed -- copied from KonqViewManager::setCurrentProfile
233 const QString localPath = KStandardDirs::locateLocal("data", QString::fromLatin1("konqueror/profiles/") +
234 profileFileName, KGlobal::mainComponent());
235 KSharedConfigPtr cfg = KSharedConfig::openConfig(localPath, KConfig::SimpleConfig);
236 if (!QFile::exists(localPath)) {
237 const QString globalFile = KStandardDirs::locate("data", QString::fromLatin1("konqueror/profiles/") +
238 profileFileName, KGlobal::mainComponent());
239 if (!globalFile.isEmpty()) {
240 KSharedConfigPtr globalCfg = KSharedConfig::openConfig(globalFile, KConfig::SimpleConfig);
241 globalCfg->copyTo(localPath, cfg.data());
244 KConfigGroup profileGroup(cfg, "Profile");
246 QString rootItem = profileGroup.readEntry("RootItem");
247 if (rootItem.isEmpty()) {
248 rootItem = "View0";
249 profileGroup.writeEntry("RootItem", rootItem);
251 QString prefix;
252 if (rootItem.startsWith("View")) {
253 prefix = rootItem + '_';
254 } else {
255 // simplify the other cases: whether root is a splitter or directly the tabwidget,
256 // we want to look at the first view inside the tabs, i.e. ViewT0.
257 prefix = "ViewT0_";
259 profileGroup.writeEntry(prefix + "URL", url);
260 profileGroup.writeEntry(prefix + "ServiceType", serviceType);
261 profileGroup.writeEntry(prefix + "ServiceName", serviceName);
262 profileGroup.sync();
265 void KKonqGeneralOptions::save()
267 KConfigGroup userSettings(m_pConfig, "UserSettings");
268 userSettings.writeEntry("HomeURL", homeURL->url().url());
269 const int startComboIndex = m_startCombo->currentIndex();
270 const int choice = m_startCombo->itemData(startComboIndex).toInt();
271 updateWebbrowsingProfile(homeURL->url().url(), static_cast<StartPage>(choice));
273 // TODO create local webbrowsing profile,
274 // look for View0_ServiceName=konq_aboutpage or ViewT0_ServiceName=khtml
275 // and replace with
276 // ViewT0_ServiceName=khtml (if http)
277 // ViewT0_ServiceType=text/html (if http)
278 // ViewT0_URL[$e]=http://www.kde.org/
280 KConfigGroup cg(m_pConfig, "FMSettings");
281 cg.writeEntry( "MMBOpensTab", tabOptions->m_pShowMMBInTabs->isChecked() );
282 cg.writeEntry( "AlwaysTabbedMode", !(tabOptions->m_pDynamicTabbarHide->isChecked()) );
284 cg.writeEntry( "NewTabsInFront", !(tabOptions->m_pNewTabsInBackground->isChecked()) );
285 cg.writeEntry( "OpenAfterCurrentPage", tabOptions->m_pOpenAfterCurrentPage->isChecked() );
286 cg.writeEntry( "PermanentCloseButton", tabOptions->m_pPermanentCloseButton->isChecked() );
287 cg.writeEntry( "KonquerorTabforExternalURL", tabOptions->m_pKonquerorTabforExternalURL->isChecked() );
288 cg.writeEntry( "PopupsWithinTabs", tabOptions->m_pPopupsWithinTabs->isChecked() );
289 cg.writeEntry( "TabCloseActivatePrevious", tabOptions->m_pTabCloseActivatePrevious->isChecked() );
290 cg.writeEntry( "MouseMiddleClickClosesTab", tabOptions->m_pMiddleClickClose->isChecked() );
291 cg.sync();
292 // It only matters whether the key is present, its value has no meaning
293 cg = KConfigGroup(m_pConfig,"Notification Messages");
294 if ( tabOptions->m_pTabConfirm->isChecked() )
295 cg.deleteEntry( "MultipleTabConfirm" );
296 else
297 cg.writeEntry( "MultipleTabConfirm", true );
298 // Send signal to all konqueror instances
299 QDBusMessage message =
300 QDBusMessage::createSignal("/KonqMain", "org.kde.Konqueror.Main", "reparseConfiguration");
301 QDBusConnection::sessionBus().send(message);
303 emit changed(false);
307 void KKonqGeneralOptions::slotChanged()
309 emit changed(true);
312 #include "generalopts.moc"