test newmaemo.org server
[stockthis] / stockthis.py
index 86fb395..31f6a4c 100644 (file)
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 #
-# StocksPy: Application to get stocks data from Yahoo Finance.
-# Version 0.1
+# StocksThis: Application to get stocks data from Yahoo Finance.
 #
 
-_version = "StockThis 0.3 beta1 rev1"
+_version = "StockThis 0.3 rev1"
 
 import urllib2
 import gtk, gobject
@@ -31,13 +30,15 @@ import settings
 import logging
 import sys
 
+from portrait import FremantleRotation
+
 import osso
 osso_c = osso.Context("net.yerga.stockthis", "0.3", False)
 
 #detect if is ran locally or not
 runningpath = sys.path[0]
 
-if '/usr/share' in runningpath:
+if '/opt/' in runningpath:
     runninglocally = False
 else:
     runninglocally = True
@@ -47,7 +48,6 @@ HOME = os.path.expanduser("~")
 settingsdb, imgdir, configdir, logfile = \
     settings.define_paths(runninglocally, HOME)
 
-
 logger = logging.getLogger('st')
 logging.basicConfig(filename=logfile,level=logging.ERROR, filemode='w')
 
@@ -88,6 +88,8 @@ class StocksPy:
         self.program.add_window(self.window)
         self.window.connect("destroy", gtk.main_quit)
 
+        FremantleRotation('StockThis', None, "0.3", 0)
+
         self.create_menu(self.window)
 
         vbox = gtk.VBox()
@@ -110,6 +112,10 @@ class StocksPy:
         self.window.add(vbox)
         self.window.show_all()
 
+        self.show_info_banner(self.window,
+            ("StockThis uses your network connection to get data.\n"
+            "Be aware of the high costs that your network provider may apply."))
+
     def create_menu(self, window):
         menu = hildon.AppMenu()
         window.set_app_menu(menu)
@@ -129,7 +135,7 @@ class StocksPy:
 
         window = hildon.StackableWindow()
         self.create_menu(window)
-        window.set_title("StockThis - " + inmodel[path][1])
+        window.set_title(inmodel[path][1])
 
         vbox = gtk.VBox()
         toolbar = self.main_toolbar(False, False, None, '', '', False)
@@ -210,7 +216,7 @@ class StocksPy:
 
         win = hildon.StackableWindow()
         self.create_menu(win)
-        win.set_title("StockThis - Quotes View - " + quote[1])
+        win.set_title(quote[1])
 
         vbox = gtk.VBox()
 
@@ -221,51 +227,75 @@ class StocksPy:
         ltitle.modify_fg(gtk.STATE_NORMAL, color)
 
         parea = hildon.PannableArea()
+        parea.set_property("mov-mode", hildon.MOVEMENT_MODE_BOTH)
 
         vbox1 = gtk.VBox()
 
         hbox = gtk.HBox()
         label = gtk.Label('')
-        label.set_markup('<b><big>Price:</big></b>')
+        label.set_markup('<b><big>%39s:</big></b>' % '<u>Price</u>')
         lprice = gtk.Label('')
-        hbox.pack_start(label, False, False, 20)
-        hbox.pack_start(lprice, False, False, 245)
+        hbox.pack_start(label, False, False, 0)
+        #if self.is_portrait():
+        hbox.pack_start(lprice, False, False, 25)
+        #else:
+        #    hbox.pack_start(lprice, False, False, 245)
+
         vbox1.pack_start(hbox, True, True, 0)
 
         hbox = gtk.HBox()
         label = gtk.Label('')
-        label.set_markup('<b><big>Change:</big></b>')
+        label.set_markup('<b><big>%35s:</big></b>' % '<u>Change</u>')
         lchange = gtk.Label('')
         lpercent = gtk.Label('')
-        hbox.pack_start(label, False, False, 20)
-        hbox.pack_start(lchange, False, False, 205)
+        hbox.pack_start(label, False, False, 0)
+        #if self.is_portrait():
+        hbox.pack_start(lchange, False, False, 25)
         hbox.pack_start(lpercent, False, False, 0)
+        #else:
+        #    hbox.pack_start(lchange, False, False, 205)
+        #    hbox.pack_start(lpercent, False, False, 0)
+
         vbox1.pack_start(hbox, True, True, 0)
 
         hbox = gtk.HBox()
         label = gtk.Label('')
-        label.set_markup('<b><big>Volume:</big></b>')
+        label.set_markup('<b><big>%35s:</big></b>' % '<u>Volume</u>')
         lvolume = gtk.Label('')
-        hbox.pack_start(label, False, False, 20)
-        hbox.pack_start(lvolume, False, False, 207)
+        hbox.pack_start(label, False, False, 0)
+        #if self.is_portrait():
+        hbox.pack_start(lvolume, False, False, 25)
+        #else:
+        #    hbox.pack_start(lvolume, False, False, 207)
+
         vbox1.pack_start(hbox, True, True, 0)
 
         hbox = gtk.HBox()
         label = gtk.Label('')
-        label.set_markup('<b><big>52 week high:</big></b>')
+        label.set_markup('<b><big>%30s:</big></b>' % '<u>52 week high</u>')
         l52whigh = gtk.Label('')
-        hbox.pack_start(label, False, False, 20)
-        hbox.pack_start(l52whigh, False, False, 125)
+        hbox.pack_start(label, False, False, 0)
+        #if self.is_portrait():
+        hbox.pack_start(l52whigh, False, False, 25)
+        #else:
+        #    hbox.pack_start(l52whigh, False, False, 125)
+
         vbox1.pack_start(hbox, True, True, 0)
 
         hbox = gtk.HBox()
         label = gtk.Label('')
-        label.set_markup('<b><big>52 week low:</big></b>')
+        label.set_markup('<b><big>%30s:</big></b>' % '<u>52 week low</u>')
         l52wlow = gtk.Label('')
-        hbox.pack_start(label, False, False, 20)
-        hbox.pack_start(l52wlow, False, False, 140)
+        hbox.pack_start(label, False, False, 0)
+        #if self.is_portrait():
+        hbox.pack_start(l52wlow, False, False, 26)
+        #else:
+        #    hbox.pack_start(l52wlow, False, False, 140)
         vbox1.pack_start(hbox, True, True, 0)
 
+        #if self.is_portrait():
+        #    hbox = gtk.VBox()
+        #else:
         hbox = gtk.HBox()
         button1 = hildon.PickerButton(fhsize, horbtn)
         data = ["50", "100", "200", "300", "400", "500", "600", "700", "800",
@@ -284,24 +314,33 @@ class StocksPy:
 
         hbox1 = gtk.HBox()
         label = gtk.Label('')
-        label.set_markup('<b><big>Shares:</big></b>')
+        label.set_markup('<b><big>%37s:</big></b>' % '<u>Shares</u>')
         lshares = gtk.Label(shares)
-        hbox1.pack_start(label, False, False, 20)
-        hbox1.pack_start(lshares, False, False, 220)
+        hbox1.pack_start(label, False, False, 0)
+        #if self.is_portrait():
+        hbox1.pack_start(lshares, False, False, 25)
+        #else:
+        #    hbox1.pack_start(lshares, False, False, 220)
 
         hbox2 = gtk.HBox()
         label = gtk.Label('')
-        label.set_markup('<b><big>Holdings Value:</big></b>')
+        label.set_markup('<b><big>%29s:</big></b>' % '<u>Holdings Value</u>')
         holdingsvalue = gtk.Label("")
-        hbox2.pack_start(label, False, False, 20)
-        hbox2.pack_start(holdingsvalue, False, False, 105)
+        hbox2.pack_start(label, False, False, 0)
+        #if self.is_portrait():
+        hbox2.pack_start(holdingsvalue, False, False, 25)
+        #else:
+        #    hbox2.pack_start(holdingsvalue, False, False, 105)
 
         hbox3 = gtk.HBox()
         label = gtk.Label('')
-        label.set_markup("<b><big>Day's Value Change:</big></b>")
+        label.set_markup("<b><big>%25s:</big></b>" % "<u>Day's Value Change</u>")
         dayvaluechange = gtk.Label("")
-        hbox3.pack_start(label, False, False, 20)
-        hbox3.pack_start(dayvaluechange, False, False, 45)
+        hbox3.pack_start(label, False, False, 0)
+        #if self.is_portrait():
+        hbox3.pack_start(dayvaluechange, False, False, 25)
+        #else:
+        #    hbox3.pack_start(dayvaluechange, False, False, 45)
 
         if not portfolio:
             vbox1.pack_start(hbox, False, False, 0)
@@ -328,6 +367,14 @@ class StocksPy:
         win.show_all()
         self.show_data(quote[0], widgets, shares)
 
+    def is_portrait(self):
+        width = gtk.gdk.screen_width()
+        height = gtk.gdk.screen_height()
+        if width > height:
+            return False
+        else:
+            return True
+
     def get_shares_from_symbol(self, symbol):
         shares = "0"
         try:
@@ -337,7 +384,7 @@ class StocksPy:
                     shares = item[2]
             return shares
         except:
-            logger.exception("Getting shares from symbol")
+            logger.exception("Getting shares from symbol: %s" % symbol)
             return shares
 
     def add_to_portfolio(self, widget, button, symbol, name):
@@ -360,7 +407,7 @@ class StocksPy:
 
             self.show_info_banner(widget, "Added to portfolio")
         except:
-            logger.exception("Adding to portfolio")
+            logger.exception("Adding to portfolio: %s, %s" % (symbol, name))
             self.show_info_banner(widget, "Error adding to portfolio")
 
 
@@ -386,7 +433,7 @@ class StocksPy:
         try:
             data = yt.get_all(symbol)
         except:
-            logger.exception("Getting data from Yahoo")
+            logger.exception("Getting data from Yahoo: %s" % symbol)
             data = {'price': 'N/A', 'change': 'N/A', 'volume':'N/A',
                     '52_week_high': 'N/A', '52_week_low': 'N/A'}
             ltitle.set_markup('<b><big>Failed to get data</big></b>')
@@ -439,7 +486,7 @@ class StocksPy:
     def show_graph_view(self, widget, symbol, name):
         win = hildon.StackableWindow()
         self.create_menu(win)
-        win.set_title("StockThis - Graph View - " + name)
+        win.set_title(name)
 
         vbox = gtk.VBox()
         toolbar = self.main_toolbar(False, True, None, '', '', False)
@@ -449,6 +496,9 @@ class StocksPy:
         self.graphs_title.modify_fg(gtk.STATE_NORMAL, color)
 
         parea = hildon.PannableArea()
+        parea.set_property("mov-mode", hildon.MOVEMENT_MODE_BOTH)
+
+        vbox1 = gtk.VBox()
 
         hbox = gtk.HBox()
         hbox.set_homogeneous(True)
@@ -473,6 +523,10 @@ class StocksPy:
         button.connect("clicked", self.show_graph, '6m', win, symbol)
         hbox.pack_start(button)
 
+        vbox1.pack_start(hbox, False, False, 0)
+        hbox = gtk.HBox()
+        hbox.set_homogeneous(True)
+
         button = hildon.Button(fhsize, horbtn)
         button.set_label('1y')
         button.connect("clicked", self.show_graph, '1y', win, symbol)
@@ -493,7 +547,6 @@ class StocksPy:
         button.connect("clicked", self.show_graph, 'max', win, symbol)
         hbox.pack_start(button)
 
-        vbox1 = gtk.VBox()
         vbox1.pack_start(hbox, False, False, 0)
 
         self.graph = gtk.Image()
@@ -543,11 +596,12 @@ class StocksPy:
             pbl.write(imgdata)
 
             pbuf = pbl.get_pixbuf()
+            pbuf = pbuf.scale_simple(475, 235, gtk.gdk.INTERP_TILES)
             pbl.close()
             self.graph.set_from_pixbuf(pbuf)
             winprogind(win, 0)
         except:
-            logger.exception("Getting graph data")
+            logger.exception("Getting graph data: %s" % url)
             winprogind(win, 0)
             self.graphs_title.set_label('Failed to get data')
             self.graph.destroy()
@@ -643,39 +697,38 @@ class StocksPy:
         if allnames == []:
             for market in marketdata.eunames:
                 for company in market:
-                    if not company in allnames:
-                        allnames.append(company)
+                    allnames.append(company)
 
             for market in marketdata.omnames:
                 for company in market:
-                    if not company in allnames:
-                        allnames.append(company)
+                    allnames.append(company)
 
             for market in marketdata.usnames:
                 for company in market:
-                    if not company in allnames:
-                        allnames.append(company)
+                    allnames.append(company)
 
         if allsymbols == []:
             for market in marketdata.eusymbols:
                 for company in market:
-                    if not company in allsymbols:
-                        allsymbols.append(company)
+                    allsymbols.append(company)
 
             for market in marketdata.omsymbols:
                 for company in market:
-                    if not company in allsymbols:
-                        allsymbols.append(company)
+                    allsymbols.append(company)
 
             for market in marketdata.ussymbols:
                 for company in market:
-                    if not company in allsymbols:
-                        allsymbols.append(company)
+                    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()
+                #print allsymbols[i], allnames[i]
+                #FIXME: repeated companies in the results list
+                #print if allnames[i]
+                #for j in new_model:
+                #    if j[1] == allnames[i]:
                 new_model.set(niter, 0, allsymbols[i], 1, allnames[i])
 
         if len(new_model) == 0:
@@ -694,7 +747,7 @@ class StocksPy:
     def show_search_screen(self, model, text):
         window = hildon.StackableWindow()
         self.create_menu(window)
-        window.set_title("StockThis - Search for " + text)
+        window.set_title("Search for " + text)
 
         vbox = gtk.VBox()
         toolbar = self.main_toolbar(False, False, None, '', '', False)
@@ -729,11 +782,12 @@ class StocksPy:
 
         win = hildon.StackableWindow()
         self.create_menu(win)
-        win.set_title("StockThis - Portfolio")
+        win.set_title("Portfolio")
 
         vbox = gtk.VBox()
 
         parea = hildon.PannableArea()
+        parea.set_property("mov-mode", hildon.MOVEMENT_MODE_BOTH)
         tv = hildon.GtkTreeView(ui_normal)
         tv.set_headers_visible(True)
         self.portfolio_model = self._create_portfolio_model(data)
@@ -883,7 +937,7 @@ class StocksPy:
             price = yt.get_price(symbol)
             return price
         except:
-            logger.exception("Getting price from Yahoo")
+            logger.exception("Getting price from Yahoo: %s" % symbol)
             return "N/A"
 
     def _create_portfolio_model(self, data):
@@ -1011,9 +1065,9 @@ class About:
         elif action == "report":
             url = "http://stockthis.garage.maemo.org/reporting.html"
         elif action == "vote":
-            url = "http://maemo.org/downloads/product/stockthis"
+            url = "http://maemo.org/downloads/product/Maemo5/stockthis"
 
-        iface.load_url(url)
+        iface.open_new_window(url)
 
     def show_info(self, widget, kind):
         if kind == 'license':