zaz-favorites/source/ZAZFavorites.py

194 lines
5.5 KiB
Python

import gettext
import uno
import unohelper
from com.sun.star.task import XJobExecutor
import easymacro as app
ID_EXTENSION = 'net.elmau.zaz.Favorites'
SERVICE = ('com.sun.star.task.Job',)
p, *_ = app.get_info_path(__file__)
path_locales = app.join(p, 'locales')
try:
lang = gettext.translation('base', path_locales, languages=[app.LANG])
lang.install()
_ = lang.gettext
except Exception as e:
from gettext import gettext as _
app.error(e)
app.debug(app.LANGUAGE)
class Controllers(object):
def __init__(self, dlg):
self.d = dlg
self.paths = []
def button_add_action(self, event):
path = app.get_file()
if not path:
return
if path in self.paths:
msg = _('Path previously added')
app.msgbox(msg, self.TITLE)
return
self.paths.append(path)
p, filename, n, e = app.get_info_path(path)
self.d.grid.add_row((filename, '', path))
self.d.grid.set_cell_tooltip(0, self.d.grid.rows-1, p)
self.d.grid.sort(0)
return
def button_save_action(self, event):
msg = _('¿You want save your favorites?')
if app.question(msg, self.TITLE):
base = 'service:net.elmau.zaz.Favorites?{}'
paths = []
submenus = []
for row in range(self.d.grid.rows):
label = '{}. {}'.format(row + 1, self.d.grid[0, row])
path = self.d.grid[2, row]
paths.append(path)
path = app._path_url(path)
sm = {'Label': label, 'CommandURL': base.format(path)}
submenus.append(sm)
sm = {'Label': '-'}
submenus.append(sm)
sm = {'Label': _('Favorites...'), 'CommandURL': base.format('config')}
submenus.append(sm)
command = 'menu.zaz.favorites'
data = {
'Label': 'Favorites',
'CommandURL': command,
'After': '.uno:RecentFileList',
'Submenu': submenus,
}
doc = app.get_document()
app.remove_menu(doc.type, 'File', command)
app.insert_menu(doc.type, 'File', **data)
app.set_config('paths', paths)
self.d.close(1)
msg = _('Favorites saved correctly')
app.msgbox(msg, self.TITLE)
return
def grid_click(self, event):
col = self.d.grid.column
row = self.d.grid.row
if col != 1:
return
msg = _('¿You want delete this file?')
if app.question(msg, self.TITLE):
path = self.d.grid[2, row]
self.paths.remove(path)
self.d.grid.remove_row(row)
return
def grid_double_click(self, event):
col = self.d.grid.column
row = self.d.grid.row
if col == -1 or row == -1 or col != 0:
return
app.msgbox(self.d.grid.get_cell_tooltip(col, row))
return
class ZAZFavorites(unohelper.Base, XJobExecutor):
cmd = 'service:' + ID_EXTENSION + '?'
IMAGES = 'images'
TITLE = _('ZAZ Favorites')
def __init__(self, ctx):
self.ctx = ctx
self.path_ext = app.get_path_extension(ID_EXTENSION)
self.IMAGES = app.join(self.path_ext, self.IMAGES)
def trigger(self, args):
if args == 'config':
return self._config()
app.open_doc(args)
return
def _config(self):
dlg = self._create_dialog()
dlg.open()
return
def _create_dialog(self):
args = {
'Name': 'dialog',
'Title': self.TITLE,
'Width': 160,
'Height': 160,
}
dlg = app.create_dialog(args)
dlg.events = Controllers(dlg)
dlg.events.TITLE = self.TITLE
args = {
'Type': 'Button',
'Name': 'button_add',
'Label': _('~Add'),
'ImageURL': app.join(self.IMAGES, 'add.png'),
'ImagePosition': 1,
'Width': 60,
'Height': 15,
'Y': 5,
'FocusOnClick': False,
}
dlg.add_control(args)
columns = (
{'Title': _('File Name'), 'MaxWidth': 120},
{'Title': 'X', 'HorizontalAlign': 1, 'Resizeable': False, 'ColumnWidth': 10},
{'Title': '', 'Resizeable': False, 'ColumnWidth': 0},
)
args = {
'Type': 'Grid',
'Name': 'grid',
'Width': 140,
'Height': 110,
'ShowRowHeader': True,
'Columns': columns
}
dlg.add_control(args)
dlg.grid.set_column_image(1, app.join(self.IMAGES, 'delete.png'))
paths = app.get_config('paths')
for path in paths:
p, filename, n, e = app.get_info_path(path)
dlg.grid.add_row((filename, '', path))
dlg.grid.set_cell_tooltip(0, dlg.grid.rows-1, p)
dlg.events.paths = paths
args = {
'Type': 'Button',
'Name': 'button_save',
'Label': _('~Save'),
'ImageURL': app.join(self.IMAGES, 'save.png'),
'ImagePosition': 1,
'Width': 60,
'Height': 15,
}
dlg.add_control(args)
dlg.button_add.center()
dlg.grid.move(dlg.button_add)
dlg.grid.center()
dlg.button_save.move(dlg.grid)
dlg.button_save.center()
return dlg
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(ZAZFavorites, ID_EXTENSION, SERVICE)