qbrowserlib::Settings as singleton; refs #167

master
Marc Wäckerlin 12 years ago
parent d191d8cc68
commit 5f39f8d261
  1. 60
      src/browser.hxx
  2. 19
      src/qbrowserlib/executor.hxx
  3. 2
      src/qbrowserlib/saveorrun.hxx
  4. 13
      src/qbrowserlib/settings.hxx

@ -68,7 +68,6 @@ class Browser: public QMainWindow, protected Ui::Browser {
_url(0), _find(new qbrowserlib::ButtonLineEdit),
_kiosk(kiosk),
_downloadManager(new DownloadManager),
_settings(mimeTypes, this, settings, !kiosk),
_errorLog(this), _logincertificate(this),
_proxy("http://swisssign.com", this),
_showErrorLog(0),
@ -77,7 +76,7 @@ class Browser: public QMainWindow, protected Ui::Browser {
_searchEngines(new QComboBox),
_bookmarkfile(bookmarkfile) {
TRC; LOG<<urls;
qbrowserlib::Executor::instance(&_settings);
qbrowserlib::Settings::instance(mimeTypes, this, settings, !kiosk);
_home = "about:blank";
if (urls.size()) _home = urls.at(0);
setupUi(this);
@ -204,7 +203,8 @@ class Browser: public QMainWindow, protected Ui::Browser {
SLOT(downloadError(QString))));
assert(connect(_downloadManager.data(), SIGNAL(metaDataChanged(QNetworkReply*)),
SLOT(metaDataChanged(QNetworkReply*))));
assert(connect(&_settings, SIGNAL(newSettings()), SLOT(newSettings())));
assert(connect(&qbrowserlib::Settings::instance(),
SIGNAL(newSettings()), SLOT(newSettings())));
newSettings();
on_actionNewTab_triggered();
@ -223,7 +223,9 @@ class Browser: public QMainWindow, protected Ui::Browser {
delete _bookmarks; _bookmarks=0;
}
if (!_kiosk &&
((!_startUrl && _settings.flag("SaveWindowState") && _settings()) ||
((!_startUrl &&
qbrowserlib::Settings::instance().flag("SaveWindowState") &&
qbrowserlib::Settings::instance()()) ||
_bookmarkfile.readable()))
loadWin();
if (urls.size()) load(urls.at(0));
@ -259,7 +261,9 @@ class Browser: public QMainWindow, protected Ui::Browser {
void closeEvent(QCloseEvent *event) {
TRC;
if (!_kiosk &&
((!_startUrl && _settings.flag("SaveWindowState") && _settings()) ||
((!_startUrl &&
qbrowserlib::Settings::instance().flag("SaveWindowState") &&
qbrowserlib::Settings::instance()()) ||
_bookmarkfile.writeable()))
saveWin();
QMainWindow::closeEvent(event);
@ -285,7 +289,7 @@ class Browser: public QMainWindow, protected Ui::Browser {
}
void load(QString page) {
_settings.replaceSearchEngine(page);
qbrowserlib::Settings::instance().replaceSearchEngine(page);
if (QUrl(page).scheme()=="") page = "http://"+page;
if (qobject_cast<QComboBox*>(_url))
qobject_cast<QComboBox*>(_url)->lineEdit()->setText(page);
@ -584,8 +588,8 @@ class Browser: public QMainWindow, protected Ui::Browser {
void newSettings() {
_searchEngines->clear();
for (qbrowserlib::Settings::SearchEngines::const_iterator
it(_settings.searchEngines().begin());
it!=_settings.searchEngines().end(); it++)
it(qbrowserlib::Settings::instance().searchEngines().begin());
it!=qbrowserlib::Settings::instance().searchEngines().end(); it++)
if (QFile(":/icons/"+it.key()).exists())
_searchEngines->addItem(QIcon(":/icons/"+it.key()), "", it.value());
else
@ -854,7 +858,7 @@ class Browser: public QMainWindow, protected Ui::Browser {
}
void on_actionSettings_triggered() {
_settings.show();
qbrowserlib::Settings::instance().show();
}
void on_actionSaveWindowState_triggered() {
@ -868,14 +872,17 @@ class Browser: public QMainWindow, protected Ui::Browser {
void saveWin() {
TRC; LOG<<"Save Window State";
saveBookmarks();
if (!_settings()) return;
if (!qbrowserlib::Settings::instance()()) return;
QStringList tabs;
for (int i(0); i<_tabs->count(); ++i)
tabs<<qobject_cast<QWebView*>(_tabs->widget(i))->url().toString();
_settings()->setValue("Window/Tabs", tabs);
_settings()->setValue("Window/CurrentTab", _tabs->currentIndex());
_settings()->setValue("Window/Geometry", saveGeometry());
_settings()->setValue("Window/WindowState", saveState());
qbrowserlib::Settings::instance()()->setValue("Window/Tabs", tabs);
qbrowserlib::Settings::instance()()
->setValue("Window/CurrentTab", _tabs->currentIndex());
qbrowserlib::Settings::instance()()
->setValue("Window/Geometry", saveGeometry());
qbrowserlib::Settings::instance()()
->setValue("Window/WindowState", saveState());
}
void saveBookmarks() {
@ -889,17 +896,17 @@ class Browser: public QMainWindow, protected Ui::Browser {
if (_bookmarkfile.writeable()) {
LOG<<"write to bookmark file"<<_bookmarkfile.name();
_bookmarkfile.write(urls);
} else if (_settings()) {
} else if (qbrowserlib::Settings::instance()()) {
LOG<<"write to settings";
_settings()->setValue("Window/Urls", urls);
qbrowserlib::Settings::instance()()->setValue("Window/Urls", urls);
} else LOG<<"bookmarks not saved";
}
void loadWin() {
TRC;
loadBookmarks();
if (!_settings()) return;
QStringList tabs(_settings()->value("Window/Tabs").toStringList());
if (!qbrowserlib::Settings::instance()()) return;
QStringList tabs(qbrowserlib::Settings::instance()()->value("Window/Tabs").toStringList());
int oldCnt(_tabs->count());
for (QStringList::iterator it(tabs.begin()); it!=tabs.end(); ++it) {
LOG<<"-------------------------------------------------------------";
@ -909,9 +916,12 @@ class Browser: public QMainWindow, protected Ui::Browser {
for (int i(0); i<oldCnt && _tabs->count()>1; ++i) _tabs->removeTab(0);
_tabs->setTabsClosable(_tabs->count()>1);
//_tabs->tabBar()->setShown(_tabs->count()>1);
_tabs->setCurrentIndex(_settings()->value("Window/CurrentTab").toInt());
restoreGeometry(_settings()->value("Window/Geometry").toByteArray());
restoreState(_settings()->value("Window/WindowState").toByteArray());
_tabs->setCurrentIndex(qbrowserlib::Settings::instance()()
->value("Window/CurrentTab").toInt());
restoreGeometry(qbrowserlib::Settings::instance()()
->value("Window/Geometry").toByteArray());
restoreState(qbrowserlib::Settings::instance()()
->value("Window/WindowState").toByteArray());
}
void loadBookmarks() {
@ -920,9 +930,10 @@ class Browser: public QMainWindow, protected Ui::Browser {
if (_bookmarkfile.readable()) {
LOG<<"load from file"<<_bookmarkfile.name();
urls = _bookmarkfile.read();
} else if (_settings()) {
} else if (qbrowserlib::Settings::instance()()) {
LOG<<"load from settings";
urls = _settings()->value("Window/Urls").toStringList();
urls = qbrowserlib::Settings::instance()()
->value("Window/Urls").toStringList();
}
urls.removeAll("");
urls.removeDuplicates();
@ -1369,7 +1380,7 @@ class Browser: public QMainWindow, protected Ui::Browser {
}
LOG<<"Filename:"<<filename;
QStringList type
(_settings.mimetype
(qbrowserlib::Settings::instance().mimetype
(reply->header(QNetworkRequest::ContentTypeHeader).toString(),
filename));
}
@ -1524,7 +1535,6 @@ class Browser: public QMainWindow, protected Ui::Browser {
QPrinter _printer;
SslClientAuthNetworkAccessManager _networkManager;
QSharedPointer<DownloadManager> _downloadManager;
qbrowserlib::Settings _settings;
ErrorLog _errorLog;
LoginCertificate _logincertificate;
gui::Proxy _proxy;

@ -31,13 +31,13 @@ namespace qbrowserlib {
private:
Executor(Settings* settings=0): _settings(settings) {} // singleton
Executor() {} // singleton
Executor(const Executor&); // singleton
public:
static Executor& instance(Settings* settings=0) {
static Executor _instance(settings);
static Executor& instance() {
static Executor _instance;
return _instance;
}
@ -46,10 +46,11 @@ namespace qbrowserlib {
for (DownloadProcesses::iterator it(_downloadProcesses.begin());
it!=_downloadProcesses.end(); ++it) {
LOG<<"cleanup:"<<it->second->fileName();
it->second->setAutoRemove(_settings && _settings->flag("CloseApps"));
it->second->setAutoRemove(qbrowserlib::Settings::instance()
.flag("CloseApps"));
delete it->second;
it->second = 0;
if (_settings && _settings->flag("CloseApps")) {
if (qbrowserlib::Settings::instance().flag("CloseApps")) {
LOG<<"terminate process";
it->first->terminate();
delete it->first;
@ -57,10 +58,6 @@ namespace qbrowserlib {
}
}
Settings* settings() {
return _settings;
}
public Q_SLOTS:
void run(QNetworkReply* reply, QString filename, QString command) {
@ -107,7 +104,8 @@ namespace qbrowserlib {
== _downloadProcesses.end()) return;
if (_downloadProcesses[qobject_cast<QProcess*>(sender())])
_downloadProcesses[qobject_cast<QProcess*>(sender())]
->setAutoRemove(_settings && _settings->flag("CloseApps"));
->setAutoRemove(qbrowserlib::Settings::instance()
.flag("CloseApps"));
delete _downloadProcesses[qobject_cast<QProcess*>(sender())];
_downloadProcesses.erase(qobject_cast<QProcess*>(sender()));
applicationFinished();
@ -117,7 +115,6 @@ namespace qbrowserlib {
typedef std::map<QProcess*, TemporaryFile*> DownloadProcesses;
DownloadProcesses _downloadProcesses;
Settings* _settings;
QNetworkReply* _reply;
QString _filename;
QString _command;

@ -60,7 +60,7 @@ namespace qbrowserlib {
TRC;
QString filename(remoteFilename());
QStringList type
(qbrowserlib::Executor::instance().settings()->mimetype
(qbrowserlib::Settings::instance().mimetype
(_reply->header(QNetworkRequest::ContentTypeHeader).toString(),
filename));
if (!type.isEmpty()) {

@ -36,6 +36,17 @@ namespace qbrowserlib {
public:
//! Singleton
static Settings& instance(MimeTypes mimetypes=MimeTypes(), QWidget* p=0,
QSettings* settings=0, bool autoWrite=true) {
static Settings _instance(mimetypes, p, settings, autoWrite);
return _instance;
}
private:
Settings(const Settings&); // singleton
Settings(MimeTypes mimetypes, QWidget* p=0,
QSettings* settings=0, bool autoWrite=true):
QDialog(p), _settings(settings),
@ -114,6 +125,8 @@ namespace qbrowserlib {
on__buttons_rejected();
}
public:
static bool DEBUG;
void setSettings(QSettings* settings) {

Loading…
Cancel
Save