~vcs-imports/kupfer/master-new

« back to all changes in this revision

Viewing changes to kupfer/launch.py

  • Committer: Ulrik Sverdrup
  • Date: 2009-07-12 17:34:00 UTC
  • Revision ID: git-v1:d632f3292a2f14dee4fda366bc1ae20ce631d5c0
launch: Save state in cache; read on __init__ and save on sched finish

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
import gtk
3
3
import gobject
4
4
from time import time
5
 
 
6
 
from kupfer import pretty
 
5
from os import path
 
6
 
 
7
import cPickle as pickle
 
8
 
 
9
from kupfer import pretty, config
 
10
from kupfer import scheduler
7
11
 
8
12
kupfer_env = "KUPFER_APP_ID"
9
13
 
39
43
                self.register = {}
40
44
                screen = wnck.screen_get_default()
41
45
                screen.get_windows_stacked()
 
46
                scheduler.GetScheduler().connect("finish", self._finish)
 
47
                self._load()
 
48
 
 
49
        def _get_filename(self):
 
50
                version = 1
 
51
                return path.join(config.get_cache_home(),
 
52
                                "application_identification_v%d.pickle" % version)
 
53
        def _load(self):
 
54
                reg = self._unpickle_register(self._get_filename())
 
55
                self.register = reg if reg else {}
 
56
        def _finish(self, sched):
 
57
                self._pickle_register(self.register, self._get_filename())
 
58
        def _unpickle_register(self, pickle_file):
 
59
                try:
 
60
                        pfile = open(pickle_file, "rb")
 
61
                except IOError, e:
 
62
                        return None
 
63
                try:
 
64
                        source = pickle.loads(pfile.read())
 
65
                        assert isinstance(source, dict), "Stored object not a dict"
 
66
                        self.output_debug("Reading from %s" % (pickle_file, ))
 
67
                except (pickle.PickleError, Exception), e:
 
68
                        source = None
 
69
                        self.output_info("Error loading %s: %s" % (pickle_file, e))
 
70
                return source
 
71
 
 
72
        def _pickle_register(self, reg, pickle_file):
 
73
                output = open(pickle_file, "wb")
 
74
                self.output_debug("Saving to %s" % (pickle_file, ))
 
75
                output.write(pickle.dumps(reg, pickle.HIGHEST_PROTOCOL))
 
76
                output.close()
 
77
                return True
 
78
 
42
79
        def _store(self, app_id, application):
43
80
                self.register[app_id] = application.get_name()
44
81
                self.output_debug("storing", app_id, "as", application.get_name())
50
87
                return self.register[app_id] == application.get_name()
51
88
 
52
89
        def launched_application(self, app_id):
 
90
                if self._has_match(app_id):
 
91
                        return
53
92
                timeout = time() + 10
54
93
                envcache = {}
55
94
                gobject.timeout_add_seconds(2, self._find_application, app_id, timeout, envcache)