linkchecker/linkcheck/gui/contextmenu.py

62 lines
2.6 KiB
Python
Raw Normal View History

2009-03-07 13:40:13 +00:00
# -*- coding: iso-8859-1 -*-
# Copyright (C) 2009-2014 Bastian Kleineidam
2009-03-07 13:40:13 +00:00
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
2009-07-24 21:58:20 +00:00
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2009-03-07 13:40:13 +00:00
from PyQt4 import QtGui
import os
import urlparse
from linkcheck.checker.fileurl import get_os_filename
2009-03-07 13:40:13 +00:00
class ContextMenu (QtGui.QMenu):
"""Show context menu."""
def __init__ (self, parent=None):
2011-02-17 18:59:02 +00:00
"""Add actions to context menu."""
2009-03-07 13:40:13 +00:00
super(ContextMenu, self).__init__(parent)
self.addAction(parent.actionViewOnline)
self.addAction(parent.actionCopyToClipboard)
2010-09-30 05:32:39 +00:00
self.addAction(parent.actionViewParentOnline)
2010-10-03 10:12:57 +00:00
self.addAction(parent.actionViewParentSource)
2010-09-30 05:32:39 +00:00
def enableFromItem (self, item):
2010-10-03 10:12:57 +00:00
"""Enable context menu actions depending on the item content."""
2010-09-30 05:32:39 +00:00
parent = self.parentWidget()
# data is an instance of CompactUrlData
2010-11-06 10:34:44 +00:00
data = item.url_data
2010-10-03 10:12:57 +00:00
# enable view online actions
2010-11-06 10:34:44 +00:00
parent.actionViewOnline.setEnabled(bool(data.url))
parent.actionViewParentOnline.setEnabled(bool(data.parent_url))
2010-10-03 10:12:57 +00:00
# enable view source actions
enable_parent_url_source = self.can_view_parent_source(data)
2010-10-03 10:12:57 +00:00
parent.actionViewParentSource.setEnabled(enable_parent_url_source)
def can_view_parent_source (self, url_data):
"""Determine if parent URL source can be retrieved."""
if not url_data.valid:
2010-10-03 10:12:57 +00:00
return False
parent = url_data.parent_url
if not parent:
2010-10-03 10:12:57 +00:00
return False
# Directory contents are dynamically generated, so it makes
# no sense in viewing/editing them.
if parent.startswith(u"file:"):
path = urlparse.urlsplit(parent)[2]
return not os.path.isdir(get_os_filename(path))
if parent.startswith((u"ftp:", u"ftps:")):
path = urlparse.urlsplit(parent)[2]
return bool(path) and not path.endswith(u'/')
# Only HTTP left
return parent.startswith((u"http:", u"https:"))