# Copyright (C) 2003 Konstantin Korikov
# 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.
#
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import qt
import chestnut_dialer.importers
import chestnut_dialer.importers.importer
import chestnut_dialer.qt_ui.importdialog
from chestnut_dialer import _
class FileNameArgWidget(qt.QHBox):
def __init__(self, arg_info, parent):
qt.QHBox.__init__(self, parent)
self.entry = qt.QLineEdit(self)
if arg_info.has_key("default"):
self.entry.setText(arg_info["default"])
btn = qt.QPushButton(_("Select File"), self)
qt.QObject.connect(btn, qt.SIGNAL("clicked()"), self.select_file_action)
def get_value(self):
return unicode(self.entry.text())
def select_file_action(self):
s = qt.QFileDialog.getOpenFileName(
self.entry.text() or None, None, self, None, _("Select File"))
if unicode(s) != "": self.entry.setText(s)
class StringArgWidget(qt.QLineEdit):
def __init__(self, arg_info, parent):
qt.QLineEdit.__init__(self, parent)
if arg_info.has_key("default"):
self.setText(arg_info["default"])
def get_value(self):
return unicode(self.text())
class OptionArgWidget(qt.QComboBox):
def __init__(self, arg_info, parent):
qt.QComboBox.__init__(self, parent)
self.values = []
for disp, value in arg_info["options"]:
self.insertItem(disp)
self.values.append(value)
if arg_info.has_key("default"):
self.setCurrentItem(self.values.index(arg_info["default"]))
def closeEvent(self, e):
self.values = None
e.accept()
def get_value(self):
return self.values[self.currentItem()]
class BooleanArgWidget(qt.QCheckBox):
def __init__(self, arg_info, parent):
qt.QCheckBox.__init__(self, "", parent)
if arg_info.has_key("default"):
self.setOn(arg_info["default"])
def get_value(self):
return self.isOn()
class IntegerArgWidget(qt.QSpinBox):
def __init__(self, arg_info, parent):
qt.QSpinBox.__init__(self, parent)
self.setMinValue(arg_info.has_key("min") and arg_info["min"] or -32000)
self.setMaxValue(arg_info.has_key("max") and arg_info["max"] or 32000)
self.setLineStep(arg_info.has_key("step") and arg_info["step"] or 1)
self.setValue(arg_info.has_key("default") and arg_info["default"] or 0)
def get_value(self):
return self.value()
class ArgBox(qt.QGrid):
args = None
tooltip = None
def __init__(self, args_info, parent):
qt.QGrid.__init__(self, 2, parent)
self.setMargin(3)
self.setSpacing(3)
self.tooltip = qt.QToolTip(self)
arg_types = {
"filename": FileNameArgWidget,
"string": StringArgWidget,
"option": OptionArgWidget,
"boolean": BooleanArgWidget,
"integer": IntegerArgWidget}
self.args = []
row = 0
for a_info in args_info:
label = qt.QLabel(
a_info.has_key("display") and a_info["display"] or a_info["name"], self)
label.setAlignment(64 + 2)
arg = arg_types[a_info["type"]](a_info, self)
self.args.append(arg)
if a_info.has_key("description"):
self.tooltip.add(label, a_info["description"])
self.tooltip.add(arg, a_info["description"])
row += 1
def closeEvent(self, e):
self.tooltip = None
e.accept()
def get_values(self):
return map(lambda a: a.get_value(), self.args)
class ImportDialog(chestnut_dialer.qt_ui.importdialog.ImportDialog):
accounts = None
update_list_action = None
importers = None
cur_importer = None
cur_impid = None
arg_cont = None
arg_box = None
def __init__(self, accounts, update_list_action, *args):
apply(chestnut_dialer.qt_ui.importdialog.ImportDialog.__init__,
(self,) + args)
self.AvaibleList.setSorting(1)
self.ImportList.setSorting(1)
ImpArgBoxLayout = self.ImpArgBox.layout()
ImpArgBoxLayout.setMargin(0)
self.arg_cont = qt.QScrollView(self.ImpArgBox)
self.arg_cont.setResizePolicy(qt.QScrollView.AutoOneFit)
ImpArgBoxLayout.addWidget(self.arg_cont)
self.accounts = accounts
self.update_list_action = update_list_action
self.importers = chestnut_dialer.importers.ls_importers()
if len(self.importers):
self.importers.sort(lambda a, b: cmp(a[1], b[1]))
for impid, prg in self.importers:
self.Importer.insertItem(prg)
self.Importer.setCurrentItem(0)
self.importer_activate(0)
qt.QObject.connect(self.Importer, qt.SIGNAL("activated(int)"),
self.importer_activate)
qt.QObject.connect(self.ApplyButton, qt.SIGNAL("clicked()"),
self.apply_action)
qt.QObject.connect(self.AddButton, qt.SIGNAL("clicked()"),
self.add_action)
qt.QObject.connect(self.RemoveButton, qt.SIGNAL("clicked()"),
self.remove_action)
qt.QObject.connect(self.OkButton, qt.SIGNAL("clicked()"),
self.ok_action)
qt.QObject.connect(self.CancelButton, qt.SIGNAL("clicked()"),
self.cancel_action)
def closeEvent(self, e):
self.accounts = None
self.update_list_action = None
e.accept()
def importer_activate(self, index):
self.AvaibleList.clear()
self.ImportList.clear()
self.cur_impid = self.importers[index][0]
if self.arg_box:
self.arg_cont.removeChild(self.arg_box)
self.arg_box.close()
self.arg_box = ArgBox(
chestnut_dialer.importers.get_importer_args_info(self.cur_impid),
self.arg_cont.viewport())
self.arg_cont.addChild(self.arg_box)
self.arg_box.show()
imp_info = chestnut_dialer.importers.get_importer_info(self.cur_impid)
if imp_info.has_key("description") and imp_info["description"]:
self.ImporterDescription.setText(imp_info["description"])
else: self.ImporterDescription.setText("")
if imp_info.has_key("author") and imp_info["author"]:
self.ImporterAuthor.setText(imp_info["author"])
else: self.ImporterAuthor.setText("")
self.StatusBar.setText(
_("Importer not created. Press Apply button for create it."))
def move_rows(self, source_list, dest_list):
i = source_list.firstChild()
while i:
next = i.nextSibling()
if i.isSelected():
qt.QListViewItem(dest_list, i.text(0), i.text(1))
source_list.takeItem(i)
i = next
def add_action(self):
self.move_rows(self.AvaibleList, self.ImportList)
def remove_action(self):
self.move_rows(self.ImportList, self.AvaibleList)
def apply_action(self):
self.AvaibleList.clear()
self.ImportList.clear()
try:
self.StatusBar.setText(_("Constructing importer..."))
self.cur_importer = chestnut_dialer.importers.get_importer(
self.cur_impid, self.arg_box.get_values())
for accname, accid in self.cur_importer.ls_accounts():
qt.QListViewItem(self.AvaibleList, "%03d" % accid, accname)
self.StatusBar.setText(_("Importer created"))
except chestnut_dialer.importers.importer.ConstructImporterException, e:
self.StatusBar.setText(
_("Failed to construct importer. Reason: %s" % e.msg))
def ok_action(self):
if self.cur_importer:
i = self.ImportList.firstChild()
while i:
acc_id = int(str(i.text(0)))
self.accounts.insert_account(
self.cur_importer.get_account(acc_id).copy())
i = i.nextSibling()
self.update_list_action()
self.close()
def cancel_action(self, *args):
self.close()
|