WebBrowser/Bookmarks/BookmarksImporters/IExplorerImporter.py

branch
QtWebEngine
changeset 4732
5ac4fc1dfc20
parent 4631
5c1a96925da4
child 5389
9b1c800daff3
equal deleted inserted replaced
4731:67d861d9e492 4732:5ac4fc1dfc20
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2012 - 2016 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing an importer for Internet Explorer bookmarks.
8 """
9
10 from __future__ import unicode_literals
11
12 import os
13
14 from PyQt5.QtCore import QCoreApplication, QDate, Qt
15
16 from .BookmarksImporter import BookmarksImporter
17
18 import UI.PixmapCache
19 import Globals
20
21
22 def getImporterInfo(id):
23 """
24 Module function to get information for the given source id.
25
26 @param id id of the browser ("chrome" or "chromium")
27 @return tuple with an icon (QPixmap), readable name (string), name of
28 the default bookmarks file (string), an info text (string),
29 a prompt (string) and the default directory of the bookmarks file
30 (string)
31 @exception ValueError raised to indicate an invalid browser ID
32 """
33 if id == "ie":
34 if Globals.isWindowsPlatform():
35 standardDir = os.path.expandvars(
36 "%USERPROFILE%\\Favorites")
37 else:
38 standardDir = ""
39 return (
40 UI.PixmapCache.getPixmap("internet_explorer.png"),
41 "Internet Explorer",
42 "",
43 QCoreApplication.translate(
44 "IExplorerImporter",
45 """Internet Explorer stores its bookmarks in the"""
46 """ <b>Favorites</b> folder This folder is usually"""
47 """ located in"""),
48 QCoreApplication.translate(
49 "IExplorerImporter",
50 """Please choose the folder to begin importing bookmarks."""),
51 standardDir,
52 )
53 else:
54 raise ValueError("Unsupported browser ID given ({0}).".format(id))
55
56
57 class IExplorerImporter(BookmarksImporter):
58 """
59 Class implementing the Chrome bookmarks importer.
60 """
61 def __init__(self, id="", parent=None):
62 """
63 Constructor
64
65 @param id source ID (string)
66 @param parent reference to the parent object (QObject)
67 """
68 super(IExplorerImporter, self).__init__(id, parent)
69
70 self.__fileName = ""
71
72 def setPath(self, path):
73 """
74 Public method to set the path of the bookmarks file or directory.
75
76 @param path bookmarks file or directory (string)
77 """
78 self.__fileName = path
79
80 def open(self):
81 """
82 Public method to open the bookmarks file.
83
84 @return flag indicating success (boolean)
85 """
86 if not os.path.exists(self.__fileName):
87 self._error = True
88 self._errorString = self.tr("Folder '{0}' does not exist.")\
89 .format(self.__fileName)
90 return False
91 if not os.path.isdir(self.__fileName):
92 self._error = True
93 self._errorString = self.tr("'{0}' is not a folder.")\
94 .format(self.__fileName)
95 return True
96
97 def importedBookmarks(self):
98 """
99 Public method to get the imported bookmarks.
100
101 @return imported bookmarks (BookmarkNode)
102 """
103 from ..BookmarkNode import BookmarkNode
104
105 folders = {}
106
107 importRootNode = BookmarkNode(BookmarkNode.Folder)
108 folders[self.__fileName] = importRootNode
109
110 for dir, subdirs, files in os.walk(self.__fileName):
111 for subdir in subdirs:
112 path = os.path.join(dir, subdir)
113 if dir in folders:
114 folder = BookmarkNode(BookmarkNode.Folder, folders[dir])
115 else:
116 folder = BookmarkNode(BookmarkNode.Folder, importRootNode)
117 folder.title = subdir.replace("&", "&&")
118 folders[path] = folder
119
120 for file in files:
121 name, ext = os.path.splitext(file)
122 if ext.lower() == ".url":
123 path = os.path.join(dir, file)
124 try:
125 f = open(path, "r")
126 contents = f.read()
127 f.close()
128 except IOError:
129 continue
130 url = ""
131 for line in contents.splitlines():
132 if line.startswith("URL="):
133 url = line.replace("URL=", "")
134 break
135 if url:
136 if dir in folders:
137 bookmark = BookmarkNode(BookmarkNode.Bookmark,
138 folders[dir])
139 else:
140 bookmark = BookmarkNode(BookmarkNode.Bookmark,
141 importRootNode)
142 bookmark.url = url
143 bookmark.title = name.replace("&", "&&")
144
145 if self._id == "ie":
146 importRootNode.title = self.tr("Internet Explorer Import")
147 else:
148 importRootNode.title = self.tr("Imported {0}")\
149 .format(QDate.currentDate().toString(Qt.SystemLocaleShortDate))
150 return importRootNode

eric ide

mercurial