def importBookmarks(self): # noqa C901 ''' @brief: Import bookmarks (it must return root folder) ''' root = BookmarkItem(BookmarkItem.Folder) root.setTitle('Opera Import') folders = [] # QList<BookmarkItem> folders.append(root) def getParent(): if folders: return folders[-1] else: return root item = None # BookmarkItem while self._lines: res = self._parseLine(self._lines.pop(0)) if res == self._StartFolder: item = BookmarkItem(BookmarkItem.Folder, getParent()) while self._lines: tok = self._parseLine(self._lines.pop(0)) if tok == self._EmptyLine: break elif tok == self._KeyValuePair and self._key == 'NAME': item.setTitle(self._value) folders.append(item) elif res == self._EndFolder: if len(folders) > 0: folders.pop(-1) elif res == self._StartUrl: item = BookmarkItem(BookmarkItem.Url, getParent()) while self._lines: tok = self._parseLine(self._lines.pop(0)) if tok == self._EmptyLine: break elif tok == self._KeyValuePair: if self._key == 'NAME': item.setTitle(self._value) elif self._key == 'URL': item.setUrl(QUrl(self._value)) elif self._key == 'DESCRIPTION': item.setDescription(self._value) elif self._key == 'SHORT NAME': item.setKeyword(self._value) elif res == self._StartSeparator: item = BookmarkItem(BookmarkItem.Separator, getParent()) while self._lines: if self._parseLine(self._lines.pop(0)) == self._EmptyLine: break elif res == self._StartDeleted: while self._lines: if self._parseLine(self._lines.pop(0)) == self._EmptyLine: break return root
def importBookmarks(self): root = BookmarkItem(BookmarkItem.Folder) root.setTitle('Firefox Import') try: items = self._importBookmarks() except peewee.DatabaseError as e: self._setError(str(e)) hash_ = {} # QHash<int, BookmarkItem> for item in items: parent = hash_.get(item.parent) bookmark = BookmarkItem(item.type, parent and parent or root) bookmark.setTitle(item.title and item.title or item.url.toString()) bookmark.setUrl(item.url) hash_[item.id] = bookmark return root
def _readDir(self, dir_, parent): ''' @param: dir_ QDir @param: parent BookmarkItem ''' for file_ in dir_.entryInfoList(QDir.Dirs | QDir.Files | QDir.NoDotAndDotDot): # file_ QFileInfo if file_.isDir(): folder = BookmarkItem(BookmarkItem.Folder, parent) folder.setTitle(file_.baseName()) folderDir = QDir(dir_) folderDir.cd(file_.baseName()) self._readDir(folderDir, folder) elif file_.isFile(): urlFile = QSettings(file_.absoluteFilePath(), QSettings.IniFormat) url = urlFile.value('InternetShortcut/URL', type=QUrl) item = BookmarkItem(BookmarkItem.Url, parent) item.setTitle(file_.baseName()) item.setUrl(url)
def _readBookmarks(self, list_, parent): ''' @param: list_ QVariantList @param: parent BookmarkItem ''' assert (parent) for entry in list_: typeString = entry['type'] if typeString == 'url': type_ = BookmarkItem.Url elif typeString == 'folder': type_ = BookmarkItem.Folder else: continue item = BookmarkItem(type_, parent) item.setTitle(entry['name']) if item.isUrl(): item.setUrl(QUrl.fromEncoded(entry['url'])) if 'children' in entry: self._readBookmarks(entry['children'], item)
def _addBookmark(self): item = BookmarkItem(BookmarkItem.Url) item.setTitle(_('New Bookmark')) item.setUrl(QUrl('http://')) self.__addBookmark(item)
def importBookmarks(self): ''' @brief: Import bookmarks (it must return root folder) ''' c = self._content c = c.replace("<DL", "<dl") c = c.replace("</DL", "</dl") c = c.replace("<DT", "<dt") c = c.replace("</DT", "</dt") c = c.replace("<P", "<p") c = c.replace("</P", "</p") c = c.replace("<A", "<a") c = c.replace("</A", "</a") c = c.replace("HREF=", "href=") c = c.replace("<H3", "<h3") c = c.replace("</H3", "</h3") c = c[:c.rfind('</dl><p>')] start = c.find("<dl><p>") root = BookmarkItem(BookmarkItem.Folder) root.setTitle("HTML Import") folders = [] # QList<BookmarkItem*> folders.append(root) while start > 0: string = bookmarks[start:] posOfFolder = string.find("<dt><h3") posOfEndFolder = string.find("</dl><p>") posOfLink = string.find("<dt><a") nearest = _min(posOfLink, _min(posOfFolder, posOfEndFolder)) if nearest == -1: break if nearest == posOfFolder: # Next is folder rx = re.compile(r"<dt><h3(.*)>(.*)</h3>") match = rx.search(string) folderName = match.group(2).strip() folder = BookmarkItem(BookmarkItem.Folder, not folders and root or folders[-1]) folder.setTitle(folderName) folders.append(folder) start += posOfFolder + len(match.group(0)) elif nearest == posOfEndFolder: # Next is end of folder if folders: folders.pop(-1) start += posOfEndFolder + 8 else: # Next is link rx = re.compile(r"<dt><a(.*)>(.*)</a>") match = rx.search(string) arguments = match.group(1) linkName = match.group(2).strip() rx2 = re.compile(r"href=\"(.*)\"") match2 = rx2.search(arguments) url = QUrl.fromEncoded(match2.group(1).strip()) start += posOfLink + len(match.group(0)) if not url or url.scheme() == "place" or url.scheme( ) == "about": continue b = BookmarkItem(BookmarkItem.Url, not folders and root or folders[-1]) b.setTitle(not linkName and url.toString() or linkName) b.setUrl(url) return root