class LoadRequest(object): # enum Operation GetOperation = 0 PostOperation = 1 def __init__(self, urlOrReq=QUrl(), op=GetOperation, data=QByteArray()): if isinstance(urlOrReq, self.__class__): otherReq = urlOrReq self._url = QUrl(otherReq._url) self._op = otherReq._op self._data = QByteArray(otherReq._data) else: url = QUrl(urlOrReq) self._url = url self._op = op self._data = data def isValid(self): return self._url.isValid() def url(self): return self._url def setUrl(self, url): self._url = url def urlString(self): ''' @return: String ''' return QUrl.fromPercentEncoding(self._url.toEncoded()) def operation(self): return self._op def setOperation(self, op): self._op = op def data(self): return self._data def setData(self, data): self._data = data def webRequest(self): ''' @return: QWebEngineHttpRequest ''' if self._op == self.GetOperation: method = QWebEngineHttpRequest.Get else: method = QWebEngineHttpRequest.Post req = QWebEngineHttpRequest(self._url, method) req.setPostData(self._data) return req
def _completeMostVisited(self): qs = HistoryDbModel.select().order_by( HistoryDbModel.count.desc()).limit(15) for history in qs: item = QStandardItem() url = QUrl(history.url) item.setText(url.toEncoded().data().decode()) item.setData(history.id, LocationCompleterModel.IdRole) item.setData(history.title, LocationCompleterModel.TitleRole) item.setData(url, LocationCompleterModel.UrlRole) item.setData(True, LocationCompleterModel.HistoryRole) self._items.append(item)
def startPrivateBrowsing(self, startUrl=QUrl()): url = startUrl act = self.sender() if isinstance(act, QAction): url = QUrl(act.data()) args = [const.MAIN_PATH] args.append('--private-browsing') args.append('--profile=%s' % ProfileManager.currentProfile()) if not url.isEmpty(): args.append(url.toEncoded().data().decode()) if not QProcess.startDetached(self.applicationFilePath(), args): print( 'MainApplication: Cannot start new browser process for private browsing!' ' %s %s' % (self.applicationFilePath(), args), file=stderr)
def _completeFromHistory(self): urlList = [] # QList<QUrl> showType = gVar.appSettings.showLocationSuggestions # Search in bookmarks if showType == self.HistoryAndBookmarks or showType == self.Bookmarks: bookmarksLimit = 20 bookmarks = gVar.app.bookmarks().searchBookmarksByString( self._searchString, bookmarksLimit) for bookmark in bookmarks: assert (bookmark.isUrl()) # Keyword bookmark replaces visit/search item if bookmark.keyword() == self._searchString: continue item = QStandardItem() item.setText(bookmark.url().toEncoded().data().decode()) item.setData(-1, LocationCompleterModel.IdRole) item.setData(bookmark.title(), LocationCompleterModel.TitleRole) item.setData(bookmark.url(), LocationCompleterModel.UrlRole) item.setData(bookmark.visitCount(), LocationCompleterModel.CountRole) item.setData(True, LocationCompleterModel.BookmarkRole) item.setData(bookmark, LocationCompleterModel.BookmarkItemRole) item.setData(self._searchString, LocationCompleterModel.SearchStringRole) urlList.append(bookmark.url()) self._items.append(item) # Sort by count self._items.sort( key=lambda item: item.data(LocationCompleterModel.CountRole), reverse=True) # Search in history if showType == self.HistoryAndBookmarks or showType == self.History: historyLimit = 20 qs = LocationCompleterModel.createHistoryQuery( self._searchString, historyLimit) for history in qs: url = QUrl(history.url) if url in urlList: continue item = QStandardItem() item.setText(url.toEncoded().data().decode()) item.setData(history.id, LocationCompleterModel.IdRole) item.setData(history.title, LocationCompleterModel.TitleRole) item.setData(url, LocationCompleterModel.UrlRole) item.setData(history.count, LocationCompleterModel.CountRole) item.setData(True, LocationCompleterModel.HistoryRole) item.setData(self._searchString, LocationCompleterModel.SearchStringRole) self._items.append(item)
class BookmarkItem(object): # enum Type Root = 0 Url = 1 Folder = 2 Separator = 3 Invalid = 4 TYPE_STR_CHOICES = ( (Url, 'url'), (Folder, 'folder'), (Separator, 'separator'), ) def __init__(self, type_, parent=None): self._type = type_ self._parent = None # BookmarkItem self._children = [] # QList<BookmarkItem> self._url = QUrl() self._title = '' self._description = '' self._keyword = '' self._icon = QIcon() self._iconTime = QTime() self._visitCount = 0 self._expanded = False self._sidebarExpanded = False if parent: parent.addChild(self) assert (self._parent == parent) def type(self): return self._type def setType(self, type_): self._type = type_ def isFolder(self): return self._type == self.Folder def isUrl(self): return self._type == self.Url def isSeparator(self): return self._type == self.Separator def parent(self): ''' @return: BookmarkItem ''' return self._parent def children(self): ''' @return: QList<BookmarkItem> ''' return self._children def icon(self): ''' @return: QIcon ''' # Cache icon for 20 seconds iconCacheTime = 20 * 1000 if self._type == self.Url: if self._iconTime.isNull( ) or self._iconTime.elapsed() > iconCacheTime: self._icon = IconProvider.iconForUrl(self._url) self._iconTime.restart() return self._icon elif self._type == self.Folder: return IconProvider.standardIcon(QStyle.SP_DirIcon) else: return QIcon() def setIcon(self, icon): self._icon = icon def urlString(self): return self._url.toEncoded().data().decode() def url(self): ''' @return: QUrl ''' return self._url def setUrl(self, url): ''' @param: url QUrl ''' self._url = url def title(self): ''' @return: QString ''' return self._title def setTitle(self, title): ''' @param: title QString ''' self._title = title def description(self): return self._description def setDescription(self, description): self._description = description def keyword(self): return self._keyword def setKeyword(self, keyword): self._keyword = keyword def visitCount(self): ''' @return: int ''' return self._visitCount def setVisitCount(self, count): self._visitCount = count def updateVisitCount(self): ''' @brief: Increments visitCount() (may also update last load time when implemented) ''' self._visitCount += 1 def isExpanded(self): if self._type == self.Root: return True return self._expanded def setExpanded(self, expanded): self._expanded = expanded def isSidebarExpanded(self): ''' @brief: Expanded state in Sidebar ''' if self._type == self.Root: return True else: return self._sidebarExpanded def setSidebarExpanded(self, expanded): self._sidebarExpanded = expanded def addChild(self, child, index=-1): ''' @param: child BookmarkItem ''' if child._parent: child._parent.removeChild(child) child._parent = self if index < 0: self._children.append(child) else: self._children.insert(index, child) def removeChild(self, child): ''' @param: child BookmarkItem ''' child._parent = None self._children.remove(child) @classmethod def typeFromString(cls, string): ''' @param: string QString @return: Type(enum int) ''' for type_, str_ in cls.TYPE_STR_CHOICES: if string == str_: return type_ else: return cls.Invalid @classmethod def typeToString(cls, type_): ''' @parma: type_ Type(enum int) @return: QString ''' for type0, str_ in cls.TYPE_STR_CHOICES: if type0 == type_: return str_ else: return 'invalid'