forked from Quitten/ulauncher-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
127 lines (106 loc) · 4.28 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import uuid
import json
import gi
import os
import subprocess
gi.require_version('Gdk', '3.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gdk, Gtk, GObject
from os.path import expanduser
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent, ItemEnterEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
from ulauncher.api.shared.action.ExtensionCustomAction import ExtensionCustomAction
class NotesExtension(Extension):
def __init__(self):
super(NotesExtension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
self.subscribe(ItemEnterEvent, ItemEnterEventListener())
home = expanduser("~")
notesFilePath = '%s/.notes' % home
if not os.path.isfile(notesFilePath):
f = open(notesFilePath, 'w')
f.close()
def saveNote(note):
note['id'] = str(uuid.uuid4())
del note['mode']
f = open(notesFilePath, 'a')
f.write('%s\n' % json.dumps(note))
f.close()
def updateNote(newNote):
notes = getNotes()
f = open(notesFilePath, 'w')
for note in notes:
if note['id'] == newNote['id']:
note['label'] = newNote['new_label']
f.write('%s\n' % json.dumps(note))
f.close()
def deleteNote(deleteNote):
notes = getNotes()
f = open(notesFilePath, 'w')
for note in notes:
if note['id'] != deleteNote['id']:
f.write('%s\n' % json.dumps(note))
f.close()
def copyToClipboard(note):
subprocess.call(['gpaste-client', 'add', note['data']])
def getNotes():
notes = []
f = open(notesFilePath, 'r')
lines = f.read().split('\n')
f.close()
for data in lines:
if data == '' or data == None:
continue
note = json.loads(data)
notes.append(note)
return notes
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
if event.get_keyword() == 'n':
resNotes = []
f = open(notesFilePath, 'r')
lines = f.read().split('\n')
f.close()
notes = getNotes()
for note in notes:
note['mode'] = ''
if event.get_argument() == 'delete' or event.get_argument() == 'del' or event.get_argument() == 'd':
note['mode'] = 'deleteNote'
elif event.get_argument() == 'copy' or event.get_argument() == 'c':
note['mode'] = 'copyToClipboard'
elif event.get_argument() is None or event.get_argument().strip() == '':
note['mode'] = 'copyToClipboard'
else:
note['mode'] = 'updateNote'
note['new_label'] = event.get_argument()
resNotes.append(ExtensionResultItem(icon='note.png',
name=note['data'],
description=note.get('label', ''),
on_enter=ExtensionCustomAction(note, keep_app_open=True)))
return RenderResultListAction(resNotes)
if event.get_keyword() == 'nn':
note = {}
note['data'] = event.get_argument()
note['mode'] = 'addNewNote'
return RenderResultListAction([ExtensionResultItem(icon='note.png',
name='New Note:',
description=note['data'],
on_enter=ExtensionCustomAction(note, keep_app_open=True))])
class ItemEnterEventListener(EventListener):
def on_event(self, event, extension):
note = event.get_data()
if note['mode'] == 'addNewNote':
saveNote(note)
elif note['mode'] == 'deleteNote':
deleteNote(note)
elif note['mode'] == 'copyToClipboard':
copyToClipboard(note)
elif note['mode'] == 'updateNote':
updateNote(note)
return HideWindowAction()
if __name__ == '__main__':
NotesExtension().run()