Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 20th, 2012  |  syntax: None  |  size: 8.82 KB  |  hits: 5  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2. # Copyright (C) 2006, Red Hat, Inc.
  3. # Copyright (C) 2009, One Laptop Per Child Association Inc
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  18.  
  19. import os
  20. import sys
  21. import time
  22. import subprocess
  23. import shutil
  24.  
  25. if os.environ.get('SUGAR_LOGGER_LEVEL', '') == 'debug':
  26.     print '%r STARTUP: Starting the shell' % time.time()
  27.     sys.stdout.flush()
  28.  
  29. import gettext
  30. import logging
  31.  
  32. import gconf
  33. import gtk
  34. import gobject
  35. import dbus.glib
  36. import wnck
  37.  
  38. try:
  39.     import xklavier
  40. except ImportError:
  41.     logging.debug('Could not load xklavier for keyboard configuration')
  42.  
  43. gtk.gdk.threads_init()
  44. dbus.glib.threads_init()
  45.  
  46. def cleanup_logs(logs_dir):
  47.     """Clean up the log directory, moving old logs into a numbered backup
  48.     directory.  We only keep `_MAX_BACKUP_DIRS` of these backup directories
  49.     around; the rest are removed."""
  50.     if not os.path.isdir(logs_dir):
  51.         os.makedirs(logs_dir)
  52.  
  53.     backup_logs = []
  54.     backup_dirs = []
  55.     for f in os.listdir(logs_dir):
  56.         path = os.path.join(logs_dir, f)
  57.         if os.path.isfile(path):
  58.             backup_logs.append(f)
  59.         elif os.path.isdir(path):
  60.             backup_dirs.append(path)
  61.  
  62.     if len(backup_dirs) > 3:
  63.         backup_dirs.sort()
  64.         root = backup_dirs[0]
  65.         for f in os.listdir(root):
  66.             os.remove(os.path.join(root, f))
  67.         os.rmdir(root)
  68.  
  69.     if len(backup_logs) > 0:
  70.         name = str(int(time.time()))
  71.         backup_dir = os.path.join(logs_dir, name)
  72.         os.mkdir(backup_dir)
  73.         for log in backup_logs:
  74.             source_path = os.path.join(logs_dir, log)
  75.             dest_path = os.path.join(backup_dir, log)
  76.             os.rename(source_path, dest_path)
  77.  
  78. def start_ui_service():
  79.     from jarabe.view.service import UIService
  80.  
  81.     ui_service = UIService()
  82.  
  83. def start_session_manager():
  84.     from jarabe.model.session import get_session_manager
  85.  
  86.     session_manager = get_session_manager()
  87.     session_manager.start()
  88.  
  89. def unfreeze_dcon_cb():
  90.     logging.debug('STARTUP: unfreeze_dcon_cb')
  91.     from jarabe.model import screen
  92.  
  93.     screen.set_dcon_freeze(0)
  94.  
  95. def setup_frame_cb():
  96.     logging.debug('STARTUP: setup_frame_cb')
  97.     from jarabe import frame
  98.     frame.get_view()
  99.  
  100. def setup_keyhandler_cb():
  101.     logging.debug('STARTUP: setup_keyhandler_cb')
  102.     from jarabe.view import keyhandler
  103.     from jarabe import frame
  104.     keyhandler.setup(frame.get_view())
  105.  
  106. def setup_journal_cb():
  107.     logging.debug('STARTUP: setup_journal_cb')
  108.     from jarabe.journal import journalactivity
  109.     journalactivity.start()
  110.  
  111. def show_software_updates_cb():
  112.     logging.debug('STARTUP: show_software_updates_cb')
  113.     if os.path.isfile(os.path.expanduser('~/.sugar-update')):
  114.         from jarabe.desktop import homewindow
  115.         home_window = homewindow.get_instance()
  116.         home_window.get_home_box().show_software_updates_alert()
  117.  
  118. def setup_notification_service_cb():
  119.     from jarabe.model import notifications
  120.     notifications.init()
  121.  
  122. def setup_file_transfer_cb():
  123.     from jarabe.model import filetransfer
  124.     filetransfer.init()
  125.  
  126. def setup_keyboard_cb():
  127.     logging.debug('STARTUP: setup_keyboard_cb')
  128.  
  129.     gconf_client = gconf.client_get_default()
  130.     have_config = False
  131.  
  132.     try:
  133.         display = gtk.gdk.display_get_default()
  134.         if display is not None:
  135.             engine = xklavier.Engine(display)
  136.         else:
  137.             logging.debug('setup_keyboard_cb: Could not get default display.')
  138.             return
  139.  
  140.         configrec = xklavier.ConfigRec()
  141.         configrec.get_from_server(engine)
  142.  
  143.         layouts = gconf_client.get_list(\
  144.             '/desktop/sugar/peripherals/keyboard/layouts', gconf.VALUE_STRING)
  145.         layouts_list = []
  146.         variants_list = []
  147.         for layout in layouts:
  148.             layouts_list.append(layout.split('(')[0])
  149.             variants_list.append(layout.split('(')[1][:-1])
  150.  
  151.         if layouts_list and variants_list:
  152.             have_config = True
  153.             configrec.set_layouts(layouts_list)
  154.             configrec.set_variants(variants_list)
  155.  
  156.         model = gconf_client.get_string(\
  157.             '/desktop/sugar/peripherals/keyboard/model')
  158.         if model:
  159.             have_config = True
  160.             configrec.set_model(model)
  161.  
  162.         options = gconf_client.get_list(\
  163.             '/desktop/sugar/peripherals/keyboard/options', gconf.VALUE_STRING)
  164.         if options:
  165.             have_config = True
  166.             configrec.set_options(options)
  167.  
  168.         if have_config:
  169.             configrec.activate(engine)
  170.     except Exception:
  171.         logging.exception('Error during keyboard configuration')
  172.  
  173. def setup_window_manager():
  174.     logging.debug('STARTUP: window_manager')
  175.  
  176.     # have to reset cursor(metacity sets it on startup)
  177.     if subprocess.call('echo $DISPLAY; xsetroot -cursor_name left_ptr', shell=True):
  178.         logging.warning('Can not reset cursor')
  179.  
  180.     if subprocess.call('metacity-message disable-keybindings',
  181.             shell=True):
  182.         logging.warning('Can not disable metacity keybindings')
  183.  
  184. def bootstrap():
  185.     setup_window_manager()
  186.  
  187.     from jarabe.view import launcher
  188.     launcher.setup()
  189.  
  190.     gobject.idle_add(setup_frame_cb)
  191.     gobject.idle_add(setup_keyhandler_cb)
  192.     gobject.idle_add(setup_journal_cb)
  193.     gobject.idle_add(setup_notification_service_cb)
  194.     gobject.idle_add(setup_file_transfer_cb)
  195.     gobject.idle_add(show_software_updates_cb)
  196.  
  197.     if sys.modules.has_key('xklavier'):
  198.         gobject.idle_add(setup_keyboard_cb)
  199.  
  200. def set_fonts():
  201.     client = gconf.client_get_default()
  202.     face = client.get_string('/desktop/sugar/font/default_face')
  203.     size = client.get_float('/desktop/sugar/font/default_size')
  204.     settings = gtk.settings_get_default()
  205.     settings.set_property("gtk-font-name", "%s %f" % (face, size))
  206.  
  207. def main():
  208.     try:
  209.         from sugar import env
  210.         # Remove temporary files. See http://bugs.sugarlabs.org/ticket/1876
  211.         data_dir = os.path.join(env.get_profile_path(), 'data')
  212.         shutil.rmtree(data_dir, ignore_errors=True)
  213.         os.makedirs(data_dir)
  214.         cleanup_logs(env.get_logs_path())
  215.     except OSError, e:
  216.         # logs cleanup is not critical; it should not prevent sugar from
  217.         # starting if (for example) the disk is full or read-only.
  218.         print 'logs cleanup failed: %s' % e
  219.  
  220.     from sugar import logger
  221.     # NOTE: This needs to happen so early because some modules register translatable
  222.     # strings in the module scope.
  223.     from jarabe import config
  224.     gettext.bindtextdomain('sugar', config.locale_path)
  225.     gettext.bindtextdomain('sugar-toolkit', config.locale_path)
  226.     gettext.textdomain('sugar')
  227.  
  228.     from jarabe.desktop import homewindow
  229.     from jarabe.model import sound
  230.     from jarabe import intro
  231.  
  232.     logger.start('shell')
  233.  
  234.     client = gconf.client_get_default()
  235.     client.set_string('/apps/metacity/general/mouse_button_modifier',
  236.                       '<Super>')
  237.  
  238.     timezone = client.get_string('/desktop/sugar/date/timezone')
  239.     if timezone is not None and timezone:
  240.         os.environ['TZ'] = timezone
  241.  
  242.     set_fonts()
  243.  
  244.     # this must be added early, so that it executes and unfreezes the screen
  245.     # even when we initially get blocked on the intro screen
  246.     gobject.idle_add(unfreeze_dcon_cb)
  247.  
  248.     # make sure we have the correct cursor in the intro screen
  249.     # TODO #3204
  250.     if subprocess.call('echo $DISPLAY; xsetroot -cursor_name left_ptr', shell=True):
  251.         logging.warning('Can not reset cursor')
  252.     intro.check_profile()
  253.  
  254.     start_ui_service()
  255.     start_session_manager()
  256.  
  257.     sound.restore()
  258.  
  259.     sys.path.append(config.ext_path)
  260.  
  261.     icons_path = os.path.join(config.data_path, 'icons')
  262.     gtk.icon_theme_get_default().append_search_path(icons_path)
  263.  
  264.     # open homewindow before window_manager to let desktop appear fast
  265.     home_window = homewindow.get_instance()
  266.     home_window.show()
  267.  
  268.     screen = wnck.screen_get_default()
  269.     screen.connect('window-manager-changed', __window_manager_changed_cb)
  270.     _check_for_window_manager(screen)
  271.  
  272.     try:
  273.         gtk.main()
  274.     except KeyboardInterrupt:
  275.         print 'Ctrl+C pressed, exiting...'
  276.  
  277.  
  278. def __window_manager_changed_cb(screen):
  279.     _check_for_window_manager(screen)
  280.  
  281.  
  282. def _check_for_window_manager(screen):
  283.     wm_name = screen.get_window_manager_name()
  284.     if wm_name is not None:
  285.         screen.disconnect_by_func(__window_manager_changed_cb)
  286.         bootstrap()
  287.  
  288.  
  289. main()