Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/bin/sugar-session
blob: fb782353ca74a1a4e73c6a660fe238c532cda0d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python
# Copyright (C) 2006, Red Hat, Inc.
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import sys
import os
if os.environ.get('SUGAR_LOGGER_LEVEL', '') == 'debug':
    import time
    print '%r STARTUP: Starting the shell' % time.time()
    sys.stdout.flush()

import gettext
import logging

import gconf
import gtk
import gobject
import dbus.glib

gtk.gdk.threads_init()
dbus.glib.threads_init()

from sugar import logger
from sugar.profile import get_profile
from sugar import env

from jarabe.desktop.homewindow import HomeWindow
from jarabe.model import sound
from jarabe.view import launcher
from jarabe import intro
from jarabe import config

def cleanup_logs():
    """Clean up the log directory, moving old logs into a numbered backup
    directory.  We only keep `_MAX_BACKUP_DIRS` of these backup directories
    around; the rest are removed."""
    logs_dir = env.get_logs_path()
    if not os.path.isdir(logs_dir):
        os.makedirs(logs_dir)

    backup_logs = []
    backup_dirs = []
    for f in os.listdir(logs_dir):
        path = os.path.join(logs_dir, f)
        if os.path.isfile(path):
            backup_logs.append(f)
        elif os.path.isdir(path):
            backup_dirs.append(path)    

    if len(backup_dirs) > 3:
        backup_dirs.sort()
        root = backup_dirs[0]
        for f in os.listdir(root):
            os.remove(os.path.join(root, f))
        os.rmdir(root)

    if len(backup_logs) > 0:
        name = str(int(time.time()))
        backup_dir = os.path.join(logs_dir, name)
        os.mkdir(backup_dir)
        for log in backup_logs:
            source_path = os.path.join(logs_dir, log)
            dest_path = os.path.join(backup_dir, log)
            os.rename(source_path, dest_path)

def start_datastore():
    from sugar.datastore import datastore

    # Mount the datastore in internal flash
    ds_path = env.get_profile_path('datastore')
    try:
        datastore.mount(ds_path, [], timeout=120)
    except Exception, e:
        # Don't explode if there's corruption; move the data out of the way
        # and attempt to create a store from scratch.
        logging.error(e)
        shutil.move(ds_path, os.path.abspath(ds_path) + str(time.time()))
        datastore.mount(ds_path, [], timeout=120)

def start_ui_service():
    from jarabe.view.service import UIService

    ui_service = UIService()
    ui_service.start()

def start_session_manager():
    from jarabe.model.session import get_session_manager

    session_manager = get_session_manager()
    session_manager.start()

def uisetup_completed_cb():
    logging.debug('STARTUP: --- uisetup_completed_cb ---')
    from jarabe.model import screen

    screen.set_dcon_freeze(0)

def uisetup_delayed_cb(home_window):
    logging.debug('STARTUP: --- uisetup_delayed_cb ---')
    from jarabe.journal import journalactivity
    from jarabe.view import keyhandler
    from jarabe import frame

    keyhandler.setup(frame.get_view())

    journalactivity.start()

    if os.path.isfile(os.path.expanduser('~/.sugar-update')):
        home_window.get_home_box().show_software_updates_alert()

def main():
    cleanup_logs()
    logger.start('shell')

    intro.check_profile()

    client = gconf.client_get_default()
    timezone = client.get_string('/desktop/sugar/date/timezone')

    if timezone is not '':    
        os.environ['TZ'] = timezone

    start_datastore()
    start_ui_service()
    start_session_manager()

    sound.restore()

    sys.path.append(config.ext_path)

    gettext.bindtextdomain('sugar', config.locale_path)
    gettext.textdomain('sugar')

    icons_path = os.path.join(config.data_path, 'icons')
    gtk.icon_theme_get_default().append_search_path(icons_path)

    launcher.setup()

    home_window = HomeWindow()
    home_window.show()

    gobject.idle_add(uisetup_completed_cb)
    gobject.idle_add(uisetup_delayed_cb, home_window)

    try:
        gtk.main()
    except KeyboardInterrupt:
        print 'Ctrl+C pressed, exiting...'

main()