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

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

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

@ -36,6 +36,17 @@ namespace qbrowserlib {
public: 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, Settings(MimeTypes mimetypes, QWidget* p=0,
QSettings* settings=0, bool autoWrite=true): QSettings* settings=0, bool autoWrite=true):
QDialog(p), _settings(settings), QDialog(p), _settings(settings),
@ -114,6 +125,8 @@ namespace qbrowserlib {
on__buttons_rejected(); on__buttons_rejected();
} }
public:
static bool DEBUG; static bool DEBUG;
void setSettings(QSettings* settings) { void setSettings(QSettings* settings) {

Loading…
Cancel
Save