+import urllib2
+import gtk
+import hildon
+import gobject
+import time
+from os.path import isfile, dirname
+import gobject
+
+class ExportOpmlData():
+ def __init__(self, parent, listing):
+ fs = hildon.FileSystemModel()
+ dialog = hildon.FileChooserDialog(parent, gtk.FILE_CHOOSER_ACTION_SAVE, fs)
+ #(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
+ #gtk.STOCK_SAVE, gtk.RESPONSE_OK))
+ #)
+ #dialog = gobject.new(hildon.FileChooserDialog, \
+ # action=gtk.FILE_CHOOSER_ACTION_SAVE)
+ #dialog.set_default_response(gtk.RESPONSE_OK)
+ #dialog.set_property('autonaming',False)
+ #dialog.set_property('show-files',True)
+ dialog.set_current_folder('/home/user/MyDocs/')
+ dialog.set_current_name('feedingit-export')
+ dialog.set_extension('opml')
+ response = dialog.run()
+ dialog.hide()
+ if response == gtk.RESPONSE_OK:
+ filename = dialog.get_filename()
+ print filename
+ try:
+
+ cont = True
+ if isfile(filename):
+ note = "File already exists. Aborted"
+ confirm = hildon.Note ("confirmation", parent, "File already exists. Are you sure you want to overwrite it?", gtk.STOCK_DIALOG_WARNING )
+ confirm.set_button_texts ("Yes", "Cancel")
+ response = confirm.run()
+ confirm.destroy()
+ if response == gtk.RESPONSE_OK:
+ cont = True
+ else:
+ note = "Operation cancelled."
+ cont = False
+ if cont:
+ file = open(filename, "w")
+ file.write(self.getOpmlText(listing))
+ file.close()
+ note = "Feeds exported to %s" %filename
+ except:
+ note = "Failed to export feeds"
+
+ dialog.destroy()
+ dialog = hildon.Note ("information", parent, note , gtk.STOCK_DIALOG_INFO )
+ dialog.run()
+ dialog.destroy()
+ elif response == gtk.RESPONSE_CANCEL:
+ dialog.destroy()
+
+ def getOpmlText(self, listing):
+ time_now = time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime())
+ opml_text = """<?xml version="1.0" encoding="UTF-8"?>
+<opml version="1.0">
+<head>
+ <title>Feeding It Export</title>
+</head>
+<body>
+"""
+ for key in listing.getListOfFeeds():
+ title = listing.getFeedTitle(key)
+ url = listing.getFeedUrl(key)
+ opml_text += """\n\t\t<outline type="rss" text="%s" title="%s" xmlUrl="%s"/>""" % (title, title, url)
+ opml_text += """\n</body>\n</opml>\n"""
+ return opml_text
+
+
+class GetOpmlData():
+ def __init__(self, parent):
+ self.parent = parent
+ dialog = hildon.Note ("confirmation", parent, "What type of OPML?", gtk.STOCK_DIALOG_WARNING )
+ dialog.set_button_texts ("File", "URL")
+ response = dialog.run()
+ dialog.destroy()
+
+ if response == gtk.RESPONSE_OK:
+ # Choose a file
+ self.data = self.askForFile()
+ else:
+ # Download a URL
+ self.data = self.downloadFile()
+
+ def getData(self):
+ if not self.data == None:
+ dialog = OpmlDialog(self.parent, self.data)
+ response = dialog.run()
+ if response == gtk.RESPONSE_ACCEPT:
+ items = dialog.getItems()
+ else:
+ items = []
+ dialog.destroy()
+ return items
+ return []
+
+ def downloadFile(self):
+ dlg = gtk.Dialog("OPML Import", self.parent, gtk.DIALOG_DESTROY_WITH_PARENT,
+ (gtk.STOCK_OK, gtk.RESPONSE_OK,
+ gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
+ lbl = gtk.Label("Enter the URL of the OPML file:")
+ lbl.show()
+ dlg.vbox.pack_start(lbl)
+ entry = gtk.Entry()
+ entry.set_text("http://")
+ entry.select_region(0,-1)
+ entry.show()
+ dlg.vbox.pack_start(entry, False)
+
+ resp = dlg.run()
+ url = entry.get_text()
+ dlg.destroy()
+ if resp == gtk.RESPONSE_CANCEL:
+ return None
+ try:
+ f = urllib2.urlopen(url)
+ data = f.read()
+ f.close()
+ except:
+ #Show error note
+ return None
+ return data
+
+ def askForFile(self):
+ #dialog = hildon.FileChooserDialog(self.parent,
+ # gtk.FILE_CHOOSER_ACTION_OPEN)
+ #dialog = gobject.new(hildon.FileChooserDialog, \
+ # action=gtk.FILE_CHOOSER_ACTION_OPEN)
+ #dialog.set_default_response(gtk.RESPONSE_OK)
+ fs = hildon.FileSystemModel()
+ dialog = hildon.FileChooserDialog(self.parent, gtk.FILE_CHOOSER_ACTION_OPEN, fs)
+
+ filter = gtk.FileFilter()
+ filter.set_name("All files")
+ filter.add_pattern("*")
+ dialog.add_filter(filter)
+
+ filter = gtk.FileFilter()
+ filter.set_name("OPML")
+ filter.add_pattern("*.xml")
+ filter.add_pattern("*.opml")
+ dialog.add_filter(filter)
+
+ response = dialog.run()
+ if response == gtk.RESPONSE_OK:
+ file = open(dialog.get_filename())
+ data = file.read()
+ file.close()
+ dialog.destroy()
+ return data
+ elif response == gtk.RESPONSE_CANCEL:
+ dialog.destroy()
+ return None
+