Merge common initialization code to gpodder.core

This commit is contained in:
Thomas Perl 2011-02-06 19:05:52 +01:00
parent 4e2abc250b
commit b20d1fe0d6
4 changed files with 85 additions and 41 deletions

View File

@ -159,7 +159,7 @@ if __name__ == '__main__':
if options.qml:
from gpodder import qtui
gpodder.ui_folders.insert(0, os.path.join(ui_folder, 'qml'))
sys.exit(qtui.main())
sys.exit(qtui.main(args))
if have_dbus:
# Try to find an already-running instance of gPodder

57
src/gpodder/core.py Normal file
View File

@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
#
# gPodder - A media aggregator and podcast client
# Copyright (c) 2005-2010 Thomas Perl and the gPodder Team
#
# gPodder is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# gPodder is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# gpodder.core - Common functionality used by all UIs
# Thomas Perl <thp@gpodder.org>; 2011-02-06
import gpodder
from gpodder import util
from gpodder import config
from gpodder import dbsqlite
from gpodder import hooks
class Core(object):
def __init__(self, \
config_class=config.Config, \
database_class=dbsqlite.Database):
# Initialize the gPodder home directory
util.make_directory(gpodder.home)
# Load installed/configured plugins
gpodder.load_plugins()
# Load hook modules and install the hook manager
user_hooks = hooks.HookManager()
if user_hooks.has_modules():
gpodder.user_hooks = user_hooks
# Open the database and configuration file
self.db = database_class(gpodder.database_file)
self.config = config_class(gpodder.config_file)
# Update the current device in the configuration
self.config.mygpo_device_type = util.detect_device_type()
def shutdown(self):
# Close the database and store outstanding changes
self.db.close()

View File

@ -66,7 +66,7 @@ except ImportError:
def __init__(self, *args, **kwargs):
pass
from gpodder import core
from gpodder import feedcore
from gpodder import util
from gpodder import opml
@ -80,7 +80,6 @@ _ = gpodder.gettext
N_ = gpodder.ngettext
from gpodder.model import Model
from gpodder.dbsqlite import Database
from gpodder.gtkui.model import PodcastListModel
from gpodder.gtkui.model import EpisodeListModel
@ -147,7 +146,7 @@ class gPodder(BuilderWidget, dbus.service.Object):
# Delay until live search is started after typing stop
LIVE_SEARCH_DELAY = 500
def __init__(self, bus_name, config):
def __init__(self, bus_name, gpodder_core):
dbus.service.Object.__init__(self, object_path=gpodder.dbus_gui_object_path, bus_name=bus_name)
self.podcasts_proxy = DBusPodcastsProxy(lambda: self.channels, \
self.on_itemUpdate_activate, \
@ -155,8 +154,9 @@ class gPodder(BuilderWidget, dbus.service.Object):
self.download_episode_list, \
self.episode_object_by_uri, \
bus_name)
self.db = Database(gpodder.database_file)
self.config = config
self.core = gpodder_core
self.config = self.core.config
self.db = self.core.db
BuilderWidget.__init__(self, None)
def new(self):
@ -2869,7 +2869,7 @@ class gPodder(BuilderWidget, dbus.service.Object):
while gtk.events_pending():
gtk.main_iteration(False)
self.db.close()
self.core.shutdown()
self.quit()
sys.exit(0)
@ -3801,20 +3801,7 @@ def main(options=None):
dlg.destroy()
sys.exit(0)
util.make_directory(gpodder.home)
gpodder.load_plugins()
config = UIConfig(gpodder.config_file)
# Load hook modules and install the hook manager globally
# if modules have been found an instantiated by the manager
user_hooks = hooks.HookManager()
if user_hooks.has_modules():
gpodder.user_hooks = user_hooks
config.mygpo_device_type = util.detect_device_type()
gp = gPodder(bus_name, config)
gp = gPodder(bus_name, core.Core(UIConfig))
# Handle options
if options.subscribe:

View File

@ -7,15 +7,13 @@ from PySide.QtCore import *
from PySide.QtDeclarative import *
from PySide.QtOpenGL import *
import sys
import os
import gpodder
from gpodder import core
from gpodder.qmlui import model
from gpodder.qmlui import helper
from gpodder import dbsqlite
from gpodder import config
from gpodder import util
# Generate a QObject subclass with notifyable properties
@ -98,10 +96,7 @@ class Controller(UiData):
@Slot()
def quit(self):
self.root.save_pending_data()
self.root._db.close() # store db
self.root.qml_view.setSource('')
self.root._app.quit()
self.root.quit()
@Slot()
def switcher(self):
@ -146,10 +141,12 @@ def QML(filename):
return filename
class qtPodder(object):
def __init__(self, args, config, db):
def __init__(self, args, gpodder_core):
self._app = QApplication(args)
self._config = config
self._db = db
self.core = gpodder_core
self._config = self.core.config
self._db = self.core.db
self.controller = Controller(self)
@ -184,6 +181,15 @@ class qtPodder(object):
self.reload_podcasts()
def run(self):
return self._app.exec_()
def quit(self):
self.save_pending_data()
self.core.shutdown()
self.qml_view.setSource('')
self._app.quit()
def set_state(self, state):
root = self.qml_view.rootObject()
root.setProperty('state', state)
@ -220,13 +226,7 @@ class qtPodder(object):
self.qml_view.rootObject().setProperty('currentEpisode', episode)
self.qml_view.rootObject().setCurrentEpisode()
def main():
gpodder.load_plugins()
cfg = config.Config(gpodder.config_file)
db = dbsqlite.Database(gpodder.database_file)
gui = qtPodder(sys.argv, cfg, db)
result = gui._app.exec_()
db.close()
print 'main finished'
return result
def main(args):
gui = qtPodder(args, core.Core())
return gui.run()