[conduit] Support for Mac OSX added to Firefox 3 Module



commit d30282d20fe8b0bacc5cb5c7d3d318c31fa63c3a
Author: astormont <astormont svn gnome org>
Date:   Fri Jul 17 08:43:17 2009 +0200

    Support for Mac OSX added to Firefox 3 Module

 ChangeLog                                        |    6 ++
 conduit/modules/ConverterModule.py               |   98 ++++++++++++++++------
 conduit/modules/Firefox3Module/Firefox3Module.py |   15 +++-
 3 files changed, 89 insertions(+), 30 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index fc1d571..a6c7eef 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2009-07-17  Andrew Stormont  <astormont svn gnome org>
+
+	* conduit/modules/ConverterModule.py:
+	* conduit/modules/Firefox3Module/Firefox3Module.py:
+        Support for Mac OSX added to Firefox 3 Module
+
 2009-07-16  Andrew Stormont  <astormont svn gnome org>
 
 	* conduit/modules/Firefox3Module/Firefox3Module.py:
diff --git a/conduit/modules/ConverterModule.py b/conduit/modules/ConverterModule.py
index 5b95278..3bd3497 100644
--- a/conduit/modules/ConverterModule.py
+++ b/conduit/modules/ConverterModule.py
@@ -1,4 +1,4 @@
-import re
+import sys, re
 import logging
 log = logging.getLogger("modules.Converter")
 
@@ -15,6 +15,8 @@ import conduit.datatypes.Bookmark as Bookmark
 
 from gettext import gettext as _
 
+from xml.dom.minidom import parseString
+
 MODULES = {
         "EmailConverter" :      { "type": "converter" },
         "NoteConverter" :       { "type": "converter" },
@@ -307,36 +309,78 @@ class BookmarkConverter(TypeConverter.Converter):
                                 uri=v
                                 )
         return bookmark
+
+    def get_bookmark_filename(self, bookmark):
+        # Return name without invalid characters
+        title = bookmark.get_title()
+        mapdict = { "/":"-",
+                    "%":"" }
+        for (old,new) in mapdict.iteritems():
+            title = title.replace(old,new)
+        return title
         
     def bookmark_to_file(self, bookmark):
-        # Now actually creates a useful filetype
-        if bookmark.get_title() != None and bookmark.get_uri() != None:
-            desktop = "[Desktop Entry]\n"
-            desktop += "Version=1.0\n"
-            desktop += "Encoding=UTF-8\n"
-            desktop += "Name=%s\n" % bookmark.get_title()
-            desktop += "Type=Link\n"
-            desktop += "URL=%s\n" % bookmark.get_uri()
-            desktop += "Icon=gnome-fs-bookmark\n"
-            f = File.TempFile(desktop)
-            f.force_new_filename(bookmark.get_title().replace("/","_") + ".desktop" )
-            return f
+        # Pick right format for OS
+        if bookmark.get_title() == None and bookmark.get_uri() == None: 
+            return
+        elif sys.platform == 'darwin':
+            return self.bookmark_to_webloc(bookmark)
+        else:
+            return self.bookmark_to_desktop(bookmark)
         
     def file_to_bookmark(self, f):
-        # This too. Now parses .desktop files... badly
+        # Pick right converter
         if f.get_mimetype().startswith("text"):
-            txt = f.get_contents_as_text()
-            title = None
-            uri = None
-            for line in txt.split("\n"):
-                if line.startswith( "Name" ):
-                    title = line.split( "=" )[1]
-                elif line.startswith( "URL" ):
-                    uri = line.split( "=" )[1]
-            if uri != None and title != None:
-                return Bookmark.Bookmark( title, uri )       
-            
-        
+            (name,extension) = f.get_filename_and_extension()
+            if extension == 'desktop':
+                return self.desktop_to_bookmark(f)
+            elif extension == 'webloc':
+                return self.webloc_to_bookmark(f)
         
+    def bookmark_to_desktop(self, bookmark):
+        # Creates .desktop file
+        desktop = "[Desktop Entry]\n"
+        desktop += "Version=1.0\n"
+        desktop += "Encoding=UTF-8\n"
+        desktop += "Name=%s\n" % bookmark.get_title()
+        desktop += "Type=Link\n"
+        desktop += "URL=%s\n" % bookmark.get_uri()
+        desktop += "Icon=gnome-fs-bookmark\n"
+        f = File.TempFile(desktop)
+        f.force_new_filename(self.get_bookmark_filename(bookmark) + ".desktop")
+        return f
         
-     
+    def desktop_to_bookmark(self, f):
+        # Parses .desktop files... badly
+        txt = f.get_contents_as_text()
+        title = None
+        uri = None
+        for line in txt.split("\n"):
+            if line.startswith( "Name" ):
+                title = line.split( "=" )[1]
+            elif line.startswith( "URL" ):
+                uri = line.split( "=" )[1]
+        if uri != None and title != None:
+            return Bookmark.Bookmark( title, uri )
+                
+    def bookmark_to_webloc(self,bookmark):
+        # Create .webloc as used by FireFox on Mac OSX
+        webloc = '<?xml version="1.0" encoding="UTF-8"?>\n'
+        webloc += '<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd";>\n'
+        webloc += '<plist version="1.0">\n'
+        webloc += '<dict>\n'
+        webloc += '    <key>URL</key>\n'
+        webloc += '    <string>%s</string>\n' % bookmark.get_uri()
+        webloc += '</dict>\n'
+        webloc += '</plist>\n'
+        f = File.TempFile(webloc)
+        f.force_new_filename(self.get_bookmark_filename(bookmark) + '.webloc')
+        return f
+
+    def webloc_to_bookmark(self,f):
+        # Parse .webloc files (easy)
+        title = f.get_filename_and_extension()[0]
+        uri = parseString(f.get_contents_as_text()).getElementsByTagName("string")[0]
+        if uri != None and title != None:
+            return Bookmark.Bookmark(title,uri)
+                
\ No newline at end of file
diff --git a/conduit/modules/Firefox3Module/Firefox3Module.py b/conduit/modules/Firefox3Module/Firefox3Module.py
index 51aa8e1..b059e50 100644
--- a/conduit/modules/Firefox3Module/Firefox3Module.py
+++ b/conduit/modules/Firefox3Module/Firefox3Module.py
@@ -14,9 +14,18 @@ import conduit.utils as Utils
 import conduit.datatypes.Bookmark as Bookmark
 import conduit.Exceptions as Exceptions
 
-FFDIR = os.path.expanduser(os.path.join("~",".mozilla","firefox"))
-
-if os.path.exists(FFDIR): 
+FFDIR = None
+LINFFDIR = os.path.expanduser(os.path.join("~",".mozilla","firefox"))
+MACFFDIR = os.path.expanduser(os.path.join("~","Library","Application Support","Firefox"))
+
+if os.path.exists(LINFFDIR):
+    FFDIR = LINFFDIR
+elif os.path.exists(MACFFDIR):
+    FFDIR = MACFFDIR
+else:
+    log.warn("Firefox 3 bookmarks support disabled")
+
+if FFDIR: 
     MODULES = {
         "Firefox3DataProviderSource" : { "type" : "dataprovider" },
     }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]