Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

remove all locks

  • Loading branch information...
commit 8f0c49ccca2a1f1340ffed352ae925a005e1d1b7 1 parent 63cb1d6
@nvbn authored
Showing with 65 additions and 67 deletions.
  1. +45 −22 everpad/pad.py
  2. +1 −19 everpad/provider.py
  3. +19 −26 everpad/utils.py
View
67 everpad/pad.py
@@ -5,12 +5,12 @@
sys.path.insert(0, '..')
from functools import partial
-from PySide.QtCore import Slot, QTranslator, QLocale
+from PySide.QtCore import Slot, QTranslator, QLocale, Signal
from PySide.QtGui import QApplication, QSystemTrayIcon, QIcon, QMenu, QMainWindow, QDialog, QMessageBox
from BeautifulSoup import BeautifulSoup
from everpad.interface.auth import Ui_Dialog as AuthDialogUi
from everpad.interface.note import Ui_MainWindow as NoteUi
-from everpad.utils import action_thread
+from everpad.utils import SyncThread
import dbus
import dbus.service
import dbus.mainloop.glib
@@ -55,6 +55,8 @@ def closeEvent(self, event):
class NoteWindow(QMainWindow):
"""Note window"""
+ note_remove = Signal()
+ note_create = Signal(str)
def __init__(self, app, note, create, *args, **kwargs):
"""Init window, connect signals with slots
@@ -105,15 +107,17 @@ def __init__(self, app, note, create, *args, **kwargs):
self.ui.actionClose_and_save.triggered.connect(self.close)
self.ui.actionClose_without_saving.triggered.connect(self.close_wo_save)
self.ui.actionRemove.triggered.connect(self.remove_note)
+ self.note_create.connect(self.note_created)
+ self.closed = False
def closeEvent(self, event):
"""Close w/wo saving"""
+ event.ignore()
self.hide()
if self.save_on_close:
self.save()
- event.ignore()
self.app.indicator.get_notes()
- self.app.opened[self.guid] = None
+ self.closed = True
@Slot()
def close_wo_save(self):
@@ -129,20 +133,30 @@ def get_data(self):
html = reduce(lambda txt, cur: txt + unicode(cur), body.contents[2:], u'')
return title, html
- @action_thread
+ @Slot(str)
+ def note_created(self, guid):
+ self.guid = guid
+ self.create = False
+ self.delete.setEnabled(True)
+
def save(self):
"""Save note"""
title, html = self.get_data()
if self.create:
- self.guid = self.app.provider.create_note(title, html)
+ self.app.sync_thread.action_receive.emit((
+ self.app.provider.create_note,
+ (title, html), {},
+ self.note_create,
+ ))
self.app.send_notify(
self.tr('Note created'),
self.tr('Note "%s" created') % title,
)
- self.create = False
- self.delete.setEnabled(True)
else:
- self.app.provider.update_note(self.guid, title, html)
+ self.app.sync_thread.action_receive.emit((
+ self.app.provider.update_note,
+ (self.guid, title, html),
+ ))
self.app.send_notify(
self.tr('Note saved'),
self.tr('Note "%s" saved') % title,
@@ -159,18 +173,16 @@ def remove_note(self):
)
ret = msgBox.exec_()
if ret == QMessageBox.Yes:
- self._remove_note()
+ self.app.sync_thread.action_receive.emit((
+ self.app.provider.remove_note,
+ (self.note[1],),
+ ))
+ self.app.send_notify(
+ self.tr('Note removed'),
+ self.tr('Note "%s" removed') % self.note[2],
+ )
self.close_wo_save()
- @action_thread
- def _remove_note(self):
- """Remove note"""
- self.app.provider.remove_note(self.note[1])
- self.app.send_notify(
- self.tr('Note removed'),
- self.tr('Note "%s" removed') % self.note[2],
- )
-
class App(QApplication):
"""Main class"""
@@ -178,6 +190,8 @@ class App(QApplication):
def __init__(self, *args, **kwargs):
"""Init app"""
QApplication.__init__(self, *args, **kwargs)
+ self.sync_thread = SyncThread()
+ self.sync_thread.start()
self.translator = QTranslator()
if not self.translator.load('i18n/%s' % QLocale.system().name()):
self.translator.load('/usr/share/everpad/lang/%s' % QLocale.system().name())
@@ -209,6 +223,7 @@ def end(self):
class Indicator(QSystemTrayIcon):
"""Indicator applet class"""
+ notes_get = Signal(list)
def __init__(self, app, *args, **kwargs):
"""Init indicator
@@ -228,11 +243,18 @@ def __init__(self, app, *args, **kwargs):
self.menu.aboutToShow.connect(self.update)
self.auth_dialog = AuthDialog(self.app)
self._notes = []
+ self.notes_get.connect(self.notes_getted)
self.get_notes()
- @action_thread
+ @Slot(list)
+ def notes_getted(self, notes):
+ self._notes = notes
+
def get_notes(self):
- self._notes = self.app.provider.get_notes('', 10)
+ self.app.sync_thread.action_receive.emit((
+ self.app.provider.get_notes,
+ ('', 10), {}, self.notes_get,
+ ))
@Slot()
def update(self):
@@ -281,7 +303,8 @@ def show_note(self, id, create=False):
note = None
else:
note = self.app.provider.get_note(id)
- if not self.app.opened.get(id, None):
+ prev = self.app.opened.get(id, None)
+ if not prev or getattr(prev, 'closed', False):
self.app.opened[id] = NoteWindow(self.app, note, create)
self.app.opened[id].show()
View
20 everpad/provider.py
@@ -12,32 +12,14 @@
from evernote.edam.type.ttypes import Note
import fcntl
from everpad.api import Api
+from everpad.utils import SyncThread
from PySide.QtCore import QCoreApplication, QTimer, Slot, Signal, QThread
import sqlite3
import os
import keyring
-class SyncThread(QThread):
- """Thread for custom actions"""
- action_receive = Signal(tuple)
- @Slot(tuple)
- def action_received(self, data):
- self.queue.append(data)
-
- def run(self):
- """Run thread with action"""
- self.queue = []
- self.action_receive.connect(self.action_received)
- while True:
- try:
- action, args, kwargs, sig = self.queue[0]
- sig.emit(action(*args, **kwargs))
- self.queue = self.queue[1:]
- except IndexError:
- time.sleep(3)
- self.exit()
class App(QCoreApplication):
View
45 everpad/utils.py
@@ -1,34 +1,27 @@
-from functools import wraps
-from PySide.QtCore import QThread
+from PySide.QtCore import QThread, Slot, Signal
+import time
-class ActionThread(QThread):
+class SyncThread(QThread):
"""Thread for custom actions"""
+ action_receive = Signal(tuple)
- def __init__(self, action, *args, **kwargs):
- """Init thread with action
+ @Slot(tuple)
+ def action_received(self, data):
+ self.queue.append(data)
- Keyword Arguments:
- action -- callable
-
- Returns: None
- """
- self.action = action
- QThread.__init__(self, *args, **kwargs)
+ def perform(self, action, args, kwargs={}, sig=None):
+ result = action(*args, **kwargs)
+ if sig:
+ sig.emit(result)
def run(self):
"""Run thread with action"""
- self.action()
+ self.queue = []
+ self.action_receive.connect(self.action_received)
+ while True:
+ try:
+ self.perform(*self.queue[0])
+ self.queue = self.queue[1:]
+ except IndexError:
+ time.sleep(3)
self.exit()
-
-def action_thread(fnc):
- @wraps(fnc)
- def wrapper(self, *args, **kwargs):
- if not hasattr(self, '_action_threads'):
- self._action_threads = []
- elif type(self._action_threads) is not list:
- raise TypeError('_action_threads mus by list')
- thread = ActionThread(lambda: fnc(self, *args, **kwargs))
- self._action_threads.append(thread)
- thread.start()
- return True
- return wrapper
Please sign in to comment.
Something went wrong with that request. Please try again.