more markets, search function, and a bunch of minor things
[stockthis] / stockthis.py
index 9de17dc..86fb395 100644 (file)
@@ -20,7 +20,7 @@
 # Version 0.1
 #
 
-_version = "StockThis 0.3 alpha1 rev1"
+_version = "StockThis 0.3 beta1 rev1"
 
 import urllib2
 import gtk, gobject
@@ -72,7 +72,8 @@ ui_normal = gtk.HILDON_UI_MODE_NORMAL
 ui_edit = gtk.HILDON_UI_MODE_EDIT
 winprogind = hildon.hildon_gtk_window_set_progress_indicator
 
-logger.debug("test this log")
+allnames = []
+allsymbols = []
 
 gtk.gdk.threads_init()
 
@@ -90,7 +91,7 @@ class StocksPy:
         self.create_menu(self.window)
 
         vbox = gtk.VBox()
-        toolbar = self.main_toolbar(False, False, None, '', '')
+        toolbar = self.main_toolbar(False, False, None, '', '', True)
 
         parea = hildon.PannableArea()
         tv = hildon.GtkTreeView(ui_normal)
@@ -131,7 +132,7 @@ class StocksPy:
         window.set_title("StockThis - " + inmodel[path][1])
 
         vbox = gtk.VBox()
-        toolbar = self.main_toolbar(False, False, None, '', '')
+        toolbar = self.main_toolbar(False, False, None, '', '', False)
 
         parea = hildon.PannableArea()
         parea.connect("horizontal-movement", self.horizontal_mov)
@@ -153,15 +154,18 @@ class StocksPy:
         #direction = 2 right-to-left
         #direction = 3 lefto-to-right
 
+        vadj = parea.get_vadjustment()
+        val = vadj.get_value()
+
         if direction == 2:
-            parea.scroll_to(-1, initial_y-400)
+            if int(val)-2500 < 0:
+                 parea.scroll_to(-1, 0)
+            else:
+                parea.scroll_to(-1, int(val)-2500)
         elif direction == 3:
-            parea.scroll_to(-1, initial_y+400)
-
-        print direction
-        print initial_x
-        print initial_y
+            parea.scroll_to(-1, int(val)+3500)
 
+        #print val
 
     def show_quotes_view(self, widget, path, column, model, portfolio):
         quote = model[path][0], model[path][1]
@@ -311,7 +315,7 @@ class StocksPy:
         widgets = [win, ltitle, lprice, lchange,  lpercent, lvolume, l52whigh,
                     l52wlow, lshares, holdingsvalue, dayvaluechange]
 
-        toolbar = self.main_toolbar(True, portfolio, widgets, quote[0], quote[1])
+        toolbar = self.main_toolbar(True, portfolio, widgets, quote[0], quote[1], False)
 
         vbox.pack_start(ltitle, False, False, 0)
         vbox.pack_start(gtk.HSeparator(), False, False, 0)
@@ -438,7 +442,7 @@ class StocksPy:
         win.set_title("StockThis - Graph View - " + name)
 
         vbox = gtk.VBox()
-        toolbar = self.main_toolbar(False, True, None, '', '')
+        toolbar = self.main_toolbar(False, True, None, '', '', False)
 
         self.graphs_title = gtk.Label(name)
         color = gtk.gdk.color_parse("#03A5FF")
@@ -563,9 +567,9 @@ class StocksPy:
             lstore.set(iter, 0, ids[item], 1, names[item])
         return lstore
 
-    def main_toolbar(self, quotesview, portfolio, widgets, symbol, name):
+    def main_toolbar(self, quotesview, portfolio, widgets, symbol, name, initial):
         toolbar = gtk.HBox()
-        toolbar.set_homogeneous(True)
+        #toolbar.set_homogeneous(True)
 
         portfolio_btn = hildon.Button(fhsize, horbtn)
         portfolio_btn.set_title("Portfolio")
@@ -580,16 +584,146 @@ class StocksPy:
         refresh_btn.connect("clicked", self.refresh_stock_data, portfolio,
                             widgets, symbol)
 
+
+        stockiconspath = "/usr/share/icons/hicolor/48x48/hildon/"
+        info_btn = hildon.Button(fhsize, horbtn)
+        img = gtk.Image()
+        img.set_from_file(stockiconspath + "general_information.png")
+        info_btn.set_image(img)
+        info_btn.connect("clicked", self.show_app_information)
+
+        search_btn = hildon.Button(fhsize, horbtn)
+        img = gtk.Image()
+        img.set_from_file(stockiconspath + "general_search.png")
+        search_btn.set_image(img)
+        search_btn.connect("clicked", self.show_search_dialog)
+
         if not portfolio:
             toolbar.pack_start(portfolio_btn)
+            if not quotesview:
+                toolbar.pack_start(info_btn, False, False, 0)
         if quotesview:
             toolbar.pack_start(graph_btn)
             toolbar.pack_start(refresh_btn)
 
+        if initial:
+            toolbar.pack_start(search_btn, False, False, 0)
+
         toolbar.show_all()
 
         return toolbar
 
+    def show_search_dialog(self, widget):
+        dlg = gtk.Dialog(title='Search company', parent=None, flags=0)
+        dlg.set_has_separator(False)
+
+        entry = hildon.Entry(fhsize)
+        dlg.vbox.pack_start(entry, False, False, 0)
+
+        button = hildon.Button(fhsize, horbtn)
+        button.set_label("Search")
+        button.connect("clicked", self.do_search, entry, dlg)
+        dlg.vbox.pack_start(button, False, False, 0)
+
+        dlg.show_all()
+        dlg.run()
+        dlg.destroy()
+
+
+    def do_search(self, widget, entry, dlg):
+        import thread
+        text = entry.get_text()
+        dlg.destroy()
+
+        winprogind(self.window, 1)
+        thread.start_new_thread(self._really_do_search, (text,))
+
+    def _really_do_search(self, text):
+
+        if allnames == []:
+            for market in marketdata.eunames:
+                for company in market:
+                    if not company in allnames:
+                        allnames.append(company)
+
+            for market in marketdata.omnames:
+                for company in market:
+                    if not company in allnames:
+                        allnames.append(company)
+
+            for market in marketdata.usnames:
+                for company in market:
+                    if not company in allnames:
+                        allnames.append(company)
+
+        if allsymbols == []:
+            for market in marketdata.eusymbols:
+                for company in market:
+                    if not company in allsymbols:
+                        allsymbols.append(company)
+
+            for market in marketdata.omsymbols:
+                for company in market:
+                    if not company in allsymbols:
+                        allsymbols.append(company)
+
+            for market in marketdata.ussymbols:
+                for company in market:
+                    if not company in allsymbols:
+                        allsymbols.append(company)
+
+        new_model = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
+        for i in range(len(allnames)):
+            if text.lower() in allnames[i].lower():
+                niter = new_model.append()
+                new_model.set(niter, 0, allsymbols[i], 1, allnames[i])
+
+        if len(new_model) == 0:
+            winprogind(self.window, 0)
+            gtk.gdk.threads_enter()
+            self.show_info_banner(self.window, "No items found for this search")
+            gtk.gdk.threads_leave()
+            return
+
+        gtk.gdk.threads_enter()
+        self.show_search_screen(new_model, text)
+        gtk.gdk.threads_leave()
+        winprogind(self.window, 0)
+
+
+    def show_search_screen(self, model, text):
+        window = hildon.StackableWindow()
+        self.create_menu(window)
+        window.set_title("StockThis - Search for " + text)
+
+        vbox = gtk.VBox()
+        toolbar = self.main_toolbar(False, False, None, '', '', False)
+
+        parea = hildon.PannableArea()
+        parea.connect("horizontal-movement", self.horizontal_mov)
+        tv = hildon.GtkTreeView(ui_normal)
+        tv.connect("row-activated", self.show_quotes_view, model, False)
+        tv.set_model(model)
+        self._tv_columns(tv)
+        parea.add(tv)
+
+        vbox.pack_start(parea, True, True, 0)
+        vbox.pack_start(gtk.HSeparator(), False, False, 5)
+        vbox.pack_start(toolbar, False, False, 0)
+
+        window.add(vbox)
+        window.show_all()
+
+
+    def show_app_information(self, widget):
+        self.show_information_note(self.window, (
+        "The data is got from Yahoo! Finance.\n"
+        "It could be delayed or even wrong.\n"
+        "The author doesn't validate in any way this data and therefore he is not responsible for any damage that may occur.\n\n"
+        "You can scroll large list with gestures:\n"
+        "Left-to-right gesture: scroll down.\n"
+        "Right-to-left gesture: scroll up."))
+
     def show_portfolio_view(self, widget):
         data = settings.load_portfolio(settingsdb)
 
@@ -786,6 +920,12 @@ class StocksPy:
         dialog.destroy()
         return False
 
+    def show_information_note(self, window, msg):
+        dialog = hildon.hildon_note_new_information(window, msg)
+        dialog.show_all()
+        result = dialog.run()
+        dialog.destroy()
+
     def show_info_banner(self, widget, msg):
         hildon.hildon_banner_show_information(widget, 'qgn_note_infoprint', msg)
 
@@ -800,9 +940,9 @@ class StocksPy:
 class About:
 
     def __init__(self, widget):
-        dialog = gtk.Dialog(title='About', parent=None, flags=0)
-        dialog.set_has_separator(False)
-        dialog.set_size_request(-1, 400)
+        self.abdialog = gtk.Dialog(title='About', parent=None, flags=0)
+        self.abdialog.set_has_separator(False)
+        self.abdialog.set_size_request(-1, 400)
 
         self.info_lb = gtk.Label()
         self.info_lb.set_line_wrap(True)
@@ -846,26 +986,34 @@ class About:
 
         self.show_info(None, 'description')
 
-        dialog.vbox.pack_start(self.action_btn, False, False, 0)
-        dialog.vbox.pack_start(self.image, False, False, 5)
-        dialog.vbox.pack_start(self.info_lb, True, True, 0)
-        dialog.vbox.pack_start(hbox1, False, False, 0)
+        self.abdialog.vbox.pack_start(self.action_btn, False, False, 0)
+        self.abdialog.vbox.pack_start(self.image, False, False, 5)
+        self.abdialog.vbox.pack_start(self.info_lb, True, True, 0)
+        self.abdialog.vbox.pack_start(hbox1, False, False, 0)
 
-        dialog.show_all()
+        self.abdialog.show_all()
         self.action_btn.hide()
         self.image.hide()
-        dialog.run()
-        dialog.destroy()
+        self.abdialog.run()
+        self.abdialog.destroy()
 
     def do_action(self, widget, action):
-        import webbrowser
+        import dbus
+
+        self.abdialog.destroy()
+
+        bus = dbus.SystemBus()
+        proxy = bus.get_object("com.nokia.osso_browser", "/com/nokia/osso_browser/request")
+        iface = dbus.Interface(proxy, 'com.nokia.osso_browser')
+
         if action == "donate":
             url = "http://stockthis.garage.maemo.org/donate.html"
         elif action == "report":
             url = "http://stockthis.garage.maemo.org/reporting.html"
         elif action == "vote":
             url = "http://maemo.org/downloads/product/stockthis"
-        webbrowser.open_new(url)
+
+        iface.load_url(url)
 
     def show_info(self, widget, kind):
         if kind == 'license':
@@ -905,7 +1053,6 @@ Developing good software takes time and hard work.
 If you like the program and it's helpful, consider donating a small amount of money.
 Donations are a great incentive and help the developer feel that the hard work is appreciated.</small>
 """
-
         elif kind == 'report':
             self.action_btn.show()
             self.image.hide()
@@ -913,11 +1060,10 @@ Donations are a great incentive and help the developer feel that the hard work i
             if self.id:
                 self.action_btn.disconnect(self.id)
             self.id = self.action_btn.connect("clicked", self.do_action, "report")
-            info = """<small>StockThis is being improved thanks to bug reports. The author appreciates very much all these reports.
+            info = """<small>StockThis is being improved thanks to bug reports that users have submitted. The author appreciates these reports.
 If the application is raising an error when you're using it, you have two choices to report this error:
 1) Send the log from the application menu (if there's an error in the log).
-2) Write a bug report in the bugtracker of StockThis with as much information as possible (especially the log from the menu).</small>"""
-
+2) Press the button and write a bug report with as much information as possible.</small>"""
         elif kind == 'vote':
             self.action_btn.show()
             self.image.show()
@@ -1030,6 +1176,20 @@ class Log:
             stockspy.show_info_banner(widget, 'Error saving the log file')
 
     def send(self, widget, dlg, logfile):
+        sendtxt = ("You are going to send the log to the developers.\n"
+        "This helps the developers to track problems with the application.\n"
+        "It doesn't send any personal information (like passwords or similar).")
+
+        dialog = hildon.hildon_note_new_confirmation(dlg, sendtxt)
+        dialog.set_button_texts("Send", "Cancel")
+        dialog.show_all()
+        response = dialog.run()
+        if response == gtk.RESPONSE_OK:
+            self.do_pre_send(dlg, logfile)
+
+        dialog.destroy()
+
+    def do_pre_send(self, dlg, logfile):
         import thread
         hildon.hildon_gtk_window_set_progress_indicator(dlg, 1)
         thread.start_new_thread(self._do_send, (dlg, logfile))