Commit 4d82d5ee authored by Tijmen de Mes's avatar Tijmen de Mes

Handle link opening if file is in cache

parent bc3b9411
......@@ -45,7 +45,7 @@ from blink.history import HistoryManager
from blink.messages import MessageManager, BlinkMessage
from blink.resources import IconManager, Resources
from blink.sessions import ChatSessionModel, ChatSessionListView, SessionManager, StreamDescription
from blink.util import run_in_gui_thread, call_later, translate
from blink.util import run_in_gui_thread, call_later, translate, copy_transfer_file
from blink.widgets.color import ColorHelperMixin
from blink.widgets.graph import Graph
from blink.widgets.otr import OTRWidget
......@@ -361,7 +361,6 @@ class ChatWebPage(QWebPage):
def __init__(self, parent=None):
super(ChatWebPage, self).__init__(parent)
self.setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
self.linkClicked.connect(QDesktopServices.openUrl)
disable_actions = {QWebPage.OpenLink, QWebPage.OpenLinkInNewWindow, QWebPage.OpenLinkInThisWindow, QWebPage.DownloadLinkToDisk,
QWebPage.OpenImageInNewWindow, QWebPage.DownloadImageToDisk, QWebPage.DownloadMediaToDisk,
QWebPage.Back, QWebPage.Forward, QWebPage.Stop, QWebPage.Reload}
......@@ -710,6 +709,8 @@ class ChatWidget(base_class, ui_class):
self.chat_input.lockReleased.connect(self._SH_ChatInputLockReleased)
self.chat_view.sizeChanged.connect(self._SH_ChatViewSizeChanged)
self.chat_view.page().mainFrame().contentsSizeChanged.connect(self._SH_ChatViewFrameContentsSizeChanged)
self.chat_view.page().linkClicked.connect(self._SH_LinkClicked)
self.chat_view.messageShouldRemove.connect(self._SH_MessageShouldRemove)
self.composing_timer.timeout.connect(self._SH_ComposingTimerTimeout)
......@@ -932,6 +933,20 @@ class ChatWidget(base_class, ui_class):
self.chat_input.keyPressEvent(QKeyEvent(QEvent.KeyPress, Qt.Key_Return, Qt.NoModifier, text='\r'))
self.chat_input.setHtml(user_text)
def _SH_LinkClicked(self, link):
directory = SIPSimpleSettings().file_transfer.directory.normalized
try:
link = copy_transfer_file(link, directory)
QDesktopServices.openUrl(link)
except FileNotFoundError:
blink_session = self.session.blink_session
id = None
if link.hasFragment():
id = link.fragment()
NotificationCenter().post_notification('BlinkSessionShouldDownloadFile',
sender=blink_session,
data=NotificationData(filename=link.fileName(), id=id))
def _SH_MessageShouldRemove(self, id):
blink_session = self.session.blink_session
MessageManager().send_remove_message(blink_session, id)
......
import os
import shutil
from PyQt5.QtCore import QObject, QThread, QTimer, QCoreApplication
from PyQt5.QtWidgets import QApplication
from application.python.decorator import decorator, preserve_signature
from application.python.descriptor import classproperty
from application.python.types import Singleton
from application.system import openfile
from filecmp import cmp
from functools import partial
from itertools import count
from threading import Event
......@@ -12,7 +16,7 @@ from sys import exc_info
from blink.event import CallFunctionEvent
__all__ = ['QSingleton', 'UniqueFilenameGenerator', 'call_in_gui_thread', 'call_later', 'run_in_gui_thread', 'translate']
__all__ = ['QSingleton', 'UniqueFilenameGenerator', 'call_in_gui_thread', 'call_later', 'copy_transfer_file', 'run_in_gui_thread', 'translate']
translate = QCoreApplication.translate
......@@ -30,6 +34,35 @@ class UniqueFilenameGenerator(object):
yield "%s-%d%s" % (prefix, x, extension)
def copy_transfer_file(link, directory):
if not link.isLocalFile():
return link
filename = link.fileName()
destination = os.path.join(directory, filename)
if destination == link.toLocalFile():
if cmp(link.toLocalFile(), destination, True):
return link
raise FileNotFoundError
is_same_file = False
for name in UniqueFilenameGenerator.generate(destination):
try:
openfile(name, 'rb')
except FileNotFoundError:
destination = name
break
else:
if cmp(link.toLocalFile(), destination, True):
is_same_file = True
break
continue
if not is_same_file:
shutil.copy(link.toLocalFile(), directory)
link.setPath(destination)
return link
def call_later(interval, function, *args, **kw):
QTimer.singleShot(int(interval*1000), lambda: function(*args, **kw))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment