src/openindiana-about.py
changeset 0 5ccf7640b396
child 1 623897dd5d0b
equal deleted inserted replaced
-1:000000000000 0:5ccf7640b396
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 #
       
     5 # Copyright (C) 2010 Oracle Corporation and/or its affiliates.
       
     6 #
       
     7 # This program is free software; you can redistribute it and/or modify
       
     8 # it under the terms of the GNU General Public License as published by
       
     9 # the Free Software Foundation; either version 2 of the License, or
       
    10 # (at your option) any later version.
       
    11 #
       
    12 # This program is distributed in the hope that it will be useful,
       
    13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    15 # GNU General Public License for more details.
       
    16 #
       
    17 # You should have received a copy of the GNU General Public License
       
    18 # along with this program; if not, write to the Free Software
       
    19 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
       
    20 #
       
    21 #
       
    22 # customizations for the OpenIndiana project added by Guido Berhoerster
       
    23 # <[email protected]>, 2010-09-03
       
    24 
       
    25 import locale
       
    26 import gettext
       
    27 from gettext import gettext as _
       
    28 import gtk, gtk.gdk
       
    29 import gnome
       
    30 import os, sys
       
    31 import string
       
    32 import re
       
    33 
       
    34 def N_(message): return message
       
    35 
       
    36 PACKAGE   = "openindiana-welcome"
       
    37 VERSION   = "Development Version"
       
    38 LOCALEDIR = "%%DATADIR%%/locale"
       
    39 PIXMAPSDIR = "%%DATADIR%%/pixmaps"
       
    40 release_string = "OpenIndiana"
       
    41 
       
    42 copyright_string = N_("Copyright 2010 The OpenIndiana Project.\nCopyright 2010 Oracle Corporation and/or its affiliates.\nAll Rights Reserved. Use is subject to license terms.")
       
    43 
       
    44 release_text = N_("Release")
       
    45 space_text = N_("Used Space")
       
    46 available_text = N_("Available Space")
       
    47 memory_text = N_("Memory")
       
    48 
       
    49 def get_machine_info():
       
    50 	# This is gross, assumes the file output is regular
       
    51 	file_buffer = os.popen("/usr/sbin/prtdiag", "r").readlines()
       
    52 	if file_buffer is None:
       
    53 		return _("Unknown")
       
    54 	else:
       
    55 		machine_line = file_buffer.pop(0)
       
    56 		machine_name = machine_line.split("System Configuration: ",1)
       
    57 		return machine_name[1][:-1]
       
    58 
       
    59 def get_machine_memory():
       
    60 	# This is also gross, assumes the file output is regular
       
    61 	file_buffer = os.popen("/usr/sbin/prtconf", "r").readlines()
       
    62 	memory_line = file_buffer.pop(1)
       
    63 	memory_info = memory_line.split("Memory size: ", 1)
       
    64 	labels = memory_info[1][:-1].split()
       
    65 	value = labels[0];
       
    66 	unit = labels[1];
       
    67 	if (re.compile("Megabytes").match(unit)):
       
    68 		return _("%.1f MB") % string.atoi(value)
       
    69 	elif (re.compile("Gigabytes").match(unit)):
       
    70 		return _("%.1f GB") % string.atoi(value)
       
    71 	else:
       
    72 		return value + " " + unit;
       
    73 
       
    74 def format_size_for_display(size):
       
    75 	KILOBYTE_FACTOR = 1024.0
       
    76 	MEGABYTE_FACTOR = (1024.0 * 1024.0)
       
    77 	GIGABYTE_FACTOR = (1024.0 * 1024.0 * 1024.0)
       
    78 
       
    79 	if size < KILOBYTE_FACTOR:
       
    80 		return _("%u bytes") % size
       
    81 	else:
       
    82 		if size < MEGABYTE_FACTOR:
       
    83 			displayed_size = size / KILOBYTE_FACTOR
       
    84 			return _("%.1f KB") % displayed_size
       
    85 		elif size < GIGABYTE_FACTOR:
       
    86 			displayed_size = size / MEGABYTE_FACTOR
       
    87 			return _("%.1f MB") % displayed_size
       
    88 		else:
       
    89 			displayed_size = size / GIGABYTE_FACTOR
       
    90 			return _("%.1f GB") % displayed_size
       
    91 class LicenseDialog( gtk.Dialog ):
       
    92 
       
    93     def __init__(self, parent, filename):
       
    94         flags = gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT
       
    95         title = _('OpenSolaris Licenses')
       
    96         buttons = (gtk.STOCK_OK, gtk.RESPONSE_OK)
       
    97         gtk.Dialog.__init__(self, title, parent, flags, buttons)
       
    98         self.connect('response', lambda w, id: self.destroy())
       
    99 
       
   100         self.set_modal(True)
       
   101         self.set_decorated(True)
       
   102         self.set_has_separator(False)
       
   103         self.set_border_width(6)
       
   104         self.set_default_size(700,700)
       
   105         self.set_resizable(True)
       
   106         self.vbox.set_spacing(12)
       
   107         #self.action_area.set_layout(gtk.BUTTONBOX_EDGE)
       
   108 
       
   109         self.scrolledwin = gtk.ScrolledWindow()
       
   110         self.scrolledwin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
       
   111         self.scrolledwin.set_shadow_type(gtk.SHADOW_ETCHED_IN)
       
   112         self.vbox.pack_start(self.scrolledwin)
       
   113 
       
   114         self.textbuffer = gtk.TextBuffer()
       
   115         self.textview = gtk.TextView(self.textbuffer)
       
   116         self.textview.set_cursor_visible(False)
       
   117         self.textview.set_editable(False)
       
   118 
       
   119 	fd = open (filename, "r")
       
   120 
       
   121 	self.iter = self.textbuffer.get_iter_at_offset(0);
       
   122 
       
   123 	for string in fd.readlines():
       
   124         	self.textbuffer.insert(self.iter, string)
       
   125 
       
   126         self.scrolledwin.add(self.textview)
       
   127         self.show_all()
       
   128 
       
   129 class DialogOpenSolaris(gtk.Dialog):
       
   130 	def __init__(self, parent=None):
       
   131         	gtk.Dialog.__init__(self, self.__class__.__name__, parent, 0, None)
       
   132 
       
   133 		self.connect('destroy', lambda *w: gtk.main_quit())
       
   134 
       
   135 		gtk.window_set_default_icon_from_file (PIXMAPSDIR + "/aboutOI.png")
       
   136 
       
   137 		# Set the dialog default spacing for about
       
   138 		self.set_title(_("Welcome to OpenSolaris"))
       
   139 		self.set_border_width(5)
       
   140 		self.set_has_separator(False)
       
   141 		self.set_resizable(False)
       
   142 		self.vbox.set_border_width(2)
       
   143 		self.action_area.set_border_width(5)
       
   144 
       
   145 		vbox = gtk.VBox(False, 8)
       
   146 		vbox.set_border_width(5)
       
   147 		self.vbox.pack_start(vbox, False, False, 0)
       
   148 
       
   149 		self.dialog = None
       
   150 
       
   151 		# Logo
       
   152 		logo = gtk.Image()
       
   153 		logo.set_from_file (PIXMAPSDIR + "/oiLogo.png")
       
   154 		vbox.pack_start(logo, False, False, 0)
       
   155 
       
   156 		# Copyright
       
   157 		copyright_label = gtk.Label()
       
   158 		copyright_label.set_markup("<span size=\"small\">%s</span>" % _(copyright_string))
       
   159 		copyright_label.set_justify(gtk.JUSTIFY_CENTER)
       
   160 		copyright_label.set_line_wrap(True)
       
   161 		hbox = gtk.HBox(True, 0)
       
   162 		hbox.pack_start(copyright_label, False, False, 12)
       
   163 		vbox.pack_start(hbox, False, False, 0)
       
   164 
       
   165 		# System Information
       
   166 
       
   167 		size_vbox = gtk.VBox(False, 0)
       
   168 		vbox.pack_end(size_vbox, False, False, 0)
       
   169 
       
   170 		vfs = os.statvfs("/")
       
   171 		size = vfs.f_blocks * vfs.f_frsize
       
   172 		avail = vfs.f_bfree * vfs.f_frsize
       
   173 		used = size - avail
       
   174 
       
   175 		# Version
       
   176 		release_label = gtk.Label()
       
   177 		release_label.set_alignment(0, 0)
       
   178 		release_label.set_markup("<span size=\"small\"><b>%s:</b></span> <span size=\"small\">%s</span>" % (_(release_text), VERSION))
       
   179 		release_label.set_justify(gtk.JUSTIFY_LEFT)
       
   180 		hbox = gtk.HBox(False, 0)
       
   181 		hbox.pack_start(release_label, False, False, 12)
       
   182 		size_vbox.pack_start(hbox, False, False, 0)
       
   183 
       
   184 		# Used Space
       
   185 		used_label = gtk.Label()
       
   186 		used_label.set_alignment(0, 0)
       
   187 		used_label.set_markup("<span size=\"small\"><b>%s:</b></span> <span size=\"small\">%s</span>" % (_(space_text), format_size_for_display(used)))
       
   188 		used_label.set_justify(gtk.JUSTIFY_LEFT)
       
   189 		hbox = gtk.HBox(False, 0)
       
   190 		hbox.pack_start(used_label, False, False, 12)
       
   191 		size_vbox.pack_start(hbox, False, False, 0)
       
   192 
       
   193 		# Available Space
       
   194 		avail_label = gtk.Label()
       
   195 		avail_label.set_alignment(0, 0)
       
   196 		avail_label.set_markup("<span size=\"small\"><b>%s:</b></span> <span size=\"small\">%s</span>" % (_(available_text), format_size_for_display(avail)))
       
   197 		avail_label.set_justify(gtk.JUSTIFY_LEFT)
       
   198 		hbox = gtk.HBox(False, 0)
       
   199 		hbox.pack_start(avail_label, False, False, 12)
       
   200 		size_vbox.pack_start(hbox, False, False, 0)
       
   201 
       
   202 		# Memory Information
       
   203 		memory_label = gtk.Label()
       
   204 		memory_label.set_alignment(0, 0)
       
   205 		memory_label.set_markup("<span size=\"small\"><b>%s:</b></span> <span size=\"small\">%s</span>" % (_(memory_text), get_machine_memory()))
       
   206 		memory_label.set_justify(gtk.JUSTIFY_LEFT)
       
   207 		hbox = gtk.HBox(False, 0)
       
   208 		hbox.pack_start(memory_label, False, False, 12)
       
   209 		size_vbox.pack_start(hbox, False, False, 0)
       
   210 
       
   211 		devices_button = gtk.Button(_("_License"), None, gtk.RESPONSE_NONE)
       
   212 		devices_button.connect('clicked', self.on_license_button_clicked)
       
   213 		self.action_area.pack_end (devices_button, False, True, 0)
       
   214 		#self.action_area.set_child_secondary(devices_button,True)
       
   215 
       
   216 		close_button=self.add_button(gtk.STOCK_CLOSE,gtk.RESPONSE_CANCEL)
       
   217 		self.set_default_response (gtk.RESPONSE_CANCEL)
       
   218 		close_button.grab_default()
       
   219 		close_button.grab_focus()
       
   220 		close_button.connect('clicked', lambda *w: gtk.main_quit())
       
   221 
       
   222 		help_button = gtk.Button(stock=gtk.STOCK_HELP)
       
   223 		help_button.connect('clicked', self.on_getting_started_button_clicked)
       
   224 		self.action_area.pack_end(help_button, False, True, 0)
       
   225 		self.action_area.set_child_secondary(help_button,True)
       
   226 
       
   227 		self.show_all()
       
   228 
       
   229 	def on_license_button_clicked (self, button):
       
   230 		# Can display using zenity or natively. Zenity ends up faster, but
       
   231 		# the window gets displayed behind the about dialog. Natively ends
       
   232 		# up being much slower.
       
   233 		# os.spawnv(os.P_NOWAIT, "/usr/bin/zenity", ["/usr/bin/zenity", "--text-info", "--width=700", "--height=700", "--title=OpenSolaris License", "--filename=/etc/notices/LICENSE"])
       
   234 		dialog = LicenseDialog(self, "/etc/notices/LICENSE")
       
   235 		dialog.run()
       
   236 		return None
       
   237 
       
   238 	def on_getting_started_button_clicked(self, button):
       
   239 		os.spawnv(os.P_NOWAIT, "/usr/bin/firefox", ["/usr/bin/firefox", "file:///usr/share/doc/openindiana-welcome/html/index.html"])
       
   240 		return None
       
   241 
       
   242 
       
   243 def main():
       
   244 	locale.setlocale (locale.LC_ALL, "")
       
   245 	gettext.textdomain (PACKAGE)
       
   246 	gettext.install (PACKAGE, LOCALEDIR)
       
   247 
       
   248 	DialogOpenSolaris()
       
   249 	gtk.main()
       
   250 
       
   251 if __name__ == '__main__':
       
   252 	main()