From 5c9bfd216fc007e9b98a598bdb0e1a91df533b12 Mon Sep 17 00:00:00 2001
From: Kalpa Welivitigoda <callkalpa@gmail.com>
Date: Sun, 21 Jul 2013 21:35:48 +0530
Subject: [PATCH 3/3] help links in a dictionary reads the current language

---
 src/jarabe/view/Makefile.am |   4 +-
 src/jarabe/view/helplink    |   3 --
 src/jarabe/view/helplink.py |  21 +++++++++
 src/jarabe/view/viewhelp.py | 104 ++++++++++++++++++++++++--------------------
 4 files changed, 81 insertions(+), 51 deletions(-)
 delete mode 100644 src/jarabe/view/helplink
 create mode 100644 src/jarabe/view/helplink.py

diff --git a/src/jarabe/view/Makefile.am b/src/jarabe/view/Makefile.am
index 8e060bd..822469a 100644
--- a/src/jarabe/view/Makefile.am
+++ b/src/jarabe/view/Makefile.am
@@ -12,4 +12,6 @@ sugar_PYTHON =				\
 	pulsingicon.py			\
 	service.py			\
 	tabbinghandler.py		\
-	viewsource.py
+	viewsource.py			\
+	viewhelp.py			\
+	helplink.py
diff --git a/src/jarabe/view/helplink b/src/jarabe/view/helplink
deleted file mode 100644
index a53c400..0000000
--- a/src/jarabe/view/helplink
+++ /dev/null
@@ -1,3 +0,0 @@
-Read: read.html
-Browse: browse.html
-Chat: chat.html
diff --git a/src/jarabe/view/helplink.py b/src/jarabe/view/helplink.py
new file mode 100644
index 0000000..f5b3500
--- /dev/null
+++ b/src/jarabe/view/helplink.py
@@ -0,0 +1,21 @@
+links = {
+    'Read': 'read.html',
+    'Browse': 'browse.html',
+    'Chat': 'chat.html',
+    'Calculate': 'calculate.html',
+    'FotoToon': 'fototoon.html',
+    'Record': 'record.html',
+    'Measure': 'measure.html',
+    'Paint': 'paint.html',
+    'Portfolio': 'portfolio.html',
+    'Memorize': 'memorize.html',
+    'Terminal': 'terminal.html',
+    'Write': 'write.html',
+    'Turtle Art': 'turtleart.html',
+    'Pippy': 'pippy.html',
+    'TamTam': 'tamtammini.html'
+}
+
+
+def get_links():
+    return links
diff --git a/src/jarabe/view/viewhelp.py b/src/jarabe/view/viewhelp.py
index 7cc9d68..f5ca889 100644
--- a/src/jarabe/view/viewhelp.py
+++ b/src/jarabe/view/viewhelp.py
@@ -24,30 +24,21 @@ from gi.repository import Gdk
 from gi.repository import WebKit
 
 from sugar3.graphics import style
-from sugar3.graphics.icon import Icon
 from sugar3.graphics.toolbutton import ToolButton
 from sugar3.bundle.activitybundle import ActivityBundle
 
 import logging
 import os
+from os.path import expanduser
+from helplink import get_links
 
-links = {}
-LINK_FILE = 'src/jarabe/view/helplink'
 _logger = logging.getLogger('ViewHelp')
-
-
-def read_links():
-    global links
-    f = open(LINK_FILE, 'r')
-    for line in f.readlines():
-        temp = line.split(':')
-        activity = temp[0].strip()
-        help_file = temp[1].strip()
-        links[activity] = help_file
+links = get_links()
+activity_path = None
 
 
 def setup_view_help(activity):
-    read_links()
+    global activity_path
     bundle_path = activity.get_bundle_path()
     if bundle_path is None:
         # display error message
@@ -58,7 +49,7 @@ def setup_view_help(activity):
 #    _logger.exception('ACTIVITY PATH:' + activity_path)
 
     if activity_name in links.keys():
-        viewhelp = ViewHelp(activity_name, links[activity_name], activity_path)
+        viewhelp = ViewHelp(activity_name, links[activity_name])
         viewhelp.show()
     else:
         # display error message
@@ -66,17 +57,17 @@ def setup_view_help(activity):
 
 
 class ViewHelp(Gtk.Window):
-    def __init__(self, activity_name, help_file, activity_path):
+    def __init__(self, activity_name, help_file):
         Gtk.Window.__init__(self)
-	box = Gtk.Box()
-	box.set_orientation(Gtk.Orientation.VERTICAL)
-	self.add(box)
-	box.show()
+        box = Gtk.Box()
+        box.set_orientation(Gtk.Orientation.VERTICAL)
+        self.add(box)
+        box.show()
 
-	toolbar = Toolbar(activity_name)
-	box.pack_start(toolbar, False, False, 0)
-	toolbar.show()
-	toolbar.connect('stop-clicked', self.stop_clicked_cb)
+        toolbar = Toolbar(activity_name)
+        box.pack_start(toolbar, False, False, 0)
+        toolbar.show()
+        toolbar.connect('stop-clicked', self.stop_clicked_cb)
 
         webview = WebKit.WebView()
         webview.set_full_content_zoom(True)
@@ -94,14 +85,33 @@ class ViewHelp(Gtk.Window):
         height = Gdk.Screen.height() - style.GRID_CELL_SIZE * 2
         self.set_size_request(width, height)
 
-        #self.add(scrolled_window)
-	box.pack_start(scrolled_window, True, True, 0)
+        box.pack_start(scrolled_window, True, True, 0)
 
         webview.show()
-        webview.load_uri('file://' + os.path.join(activity_path, 'Help.activity/help/' + help_file))
+        language = self.get_current_language()
+        locale_dir = self.get_locale_directory(language)
+        #_logger.exception('LOCALE_DIR: ', locale_dir)
+        webview.load_uri('file://' + os.path.join(locale_dir + help_file))
 
     def stop_clicked_cb(self, widget):
-    	self.destroy()
+        self.destroy()
+
+    def get_current_language(self):
+        language_file_path = os.path.join(expanduser('~'), '.i18n')
+        f = open(language_file_path, 'r')
+        language = f.readlines()[0].split('=')[1]
+        language = language.split('.')[0]
+        f.close()
+        return language
+
+    def get_locale_directory(self, language):
+        path = os.path.join(activity_path, 'Help.activity/help/')
+        #locale_path = os.path.join(path, language)
+        #_logger.exception('LOCALE PATH: ' + locale_path)
+        #if os.path.isdir(locale_path):
+        #return locale_path
+        return path
+
 
 class Toolbar(Gtk.Toolbar):
 
@@ -112,25 +122,25 @@ class Toolbar(Gtk.Toolbar):
     def __init__(self, activity_name):
         Gtk.Toolbar.__init__(self)
 
-	title = 'Help: ' + activity_name
+        title = 'Help: ' + activity_name
 
-	self.add_separator(False)
+        self.add_separator(False)
 
         label = Gtk.Label()
-	label.set_markup('<b>%s</b>' % title)
-	label.set_alignment(0, 0.5)
+        label.set_markup('<b>%s</b>' % title)
+        label.set_alignment(0, 0.5)
         self.add_widget(label)
 
-	self.add_separator(True)
+        self.add_separator(True)
 
-	stop = ToolButton(icon_name='dialog-cancel')
-	stop.set_tooltip(_('Close'))
-	stop.connect('clicked', self.stop_clicked_cb)
-	self.insert(stop, -1)
-	stop.show()
+        stop = ToolButton(icon_name='dialog-cancel')
+        stop.set_tooltip(_('Close'))
+        stop.connect('clicked', self.stop_clicked_cb)
+        self.insert(stop, -1)
+        stop.show()
 
     def stop_clicked_cb(self, widget):
-   	self.emit('stop-clicked')
+        self.emit('stop-clicked')
 
     def add_widget(self, widget):
         tool_item = Gtk.ToolItem()
@@ -140,11 +150,11 @@ class Toolbar(Gtk.Toolbar):
         tool_item.show()
 
     def add_separator(self, expand=False):
-    	separator = Gtk.SeparatorToolItem()
-	separator.props.draw = False
-	if expand:
-	    separator.set_expand(True)
-	else:
-	    separator.set_size_request(style.DEFAULT_SPACING, -1)
-	self.insert(separator, -1)
-	separator.show()
+        separator = Gtk.SeparatorToolItem()
+        separator.props.draw = False
+        if expand:
+            separator.set_expand(True)
+        else:
+            separator.set_size_request(style.DEFAULT_SPACING, -1)
+        self.insert(separator, -1)
+        separator.show()
-- 
1.8.1.4

