Skip to content

Commit

Permalink
Now preference dialog is mapped with GSettings, new GSettings schema …
Browse files Browse the repository at this point in the history
…created.
  • Loading branch information
dnohales committed Jan 18, 2012
1 parent 036e320 commit a4b7b78
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 30 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Expand Up @@ -20,4 +20,10 @@ include(ValaVersion)
find_package(Vala)
ensure_vala_version("0.11.0" MINIMUM)

if(NOT DEFINED USE_GSETTINGS)
set(USE_GSETTINGS 1)
endif()

add_subdirectory(src)

unset(USE_GSETTINGS)
1 change: 1 addition & 0 deletions config.h.in
Expand Up @@ -4,3 +4,4 @@
#define GETTEXT_PACKAGE "@GETTEXT_PACKAGE@"
#define LOCALEDIR "@LOCALEDIR@"
#define DATADIR "@DATADIR@"
#define USE_GSETTINGS @USE_GSETTINGS@
12 changes: 12 additions & 0 deletions data/es.nohal.Gablabel.gschema.xml
@@ -0,0 +1,12 @@
<schemalist>
<schema id="es.nohal.Gablabel" path="/apps/gablabel/">
<key name="clipboard-binding" type="s">
<default>"&lt;Ctrl&gt;&lt;Alt&gt;W"</default>
<summary>Key binding for translate clipboard text</summary>
</key>
<key name="selected-binding" type="s">
<default>"&lt;Ctrl&gt;&lt;Alt&gt;S"</default>
<summary>Key binding for translate selected text</summary>
</key>
</schema>
</schemalist>
3 changes: 2 additions & 1 deletion data/gablabel/mainwindow.ui
Expand Up @@ -68,6 +68,7 @@
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
Expand Down Expand Up @@ -102,7 +103,7 @@ translator does not work well</property>
</packing>
</child>
<child>
<object class="GtkToolButton" id="toolbutton_about">
<object class="GtkToolButton" id="toolbutton_preferences">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
Expand Down
10 changes: 5 additions & 5 deletions data/gablabel/preferencesdialog.ui
Expand Up @@ -6,6 +6,7 @@
<property name="can_focus">False</property>
<property name="border_width">5</property>
<property name="title" translatable="yes">Gablabel preferences</property>
<property name="destroy_with_parent">True</property>
<property name="type_hint">dialog</property>
<child internal-child="vbox">
<object class="GtkVBox" id="dialog-vbox1">
Expand Down Expand Up @@ -57,7 +58,7 @@
</packing>
</child>
<child>
<object class="GtkEntry" id="entry1">
<object class="GtkEntry" id="entry_clipboard">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">●</property>
Expand All @@ -73,7 +74,7 @@
</packing>
</child>
<child>
<object class="GtkEntry" id="entry2">
<object class="GtkEntry" id="entry_selected">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">●</property>
Expand Down Expand Up @@ -111,7 +112,6 @@
</child>
<child>
<object class="GtkAlignment" id="alignment2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="top_padding">10</property>
<property name="left_padding">5</property>
Expand Down Expand Up @@ -187,7 +187,7 @@
<property name="can_focus">False</property>
<property name="layout_style">end</property>
<child>
<object class="GtkButton" id="button1">
<object class="GtkButton" id="button_close">
<property name="label">gtk-close</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
Expand All @@ -211,7 +211,7 @@
</object>
</child>
<action-widgets>
<action-widget response="0">button1</action-widget>
<action-widget response="0">button_close</action-widget>
</action-widgets>
</object>
</interface>
59 changes: 43 additions & 16 deletions src/Application.vala
Expand Up @@ -8,18 +8,15 @@ namespace Gablabel
return Application.singleton_instance;
}

enum BindingName {
CLIPBOARD,
SELECTED
}

public MainWindow window { get; private set; }
public StatusIconManager statusIcon { get; private set; }
public KeyBindingManager keyBindings { get; private set; }
public GLib.Settings settings { get; private set; }
private Gee.Map<Application.BindingName, string> currentBindings = new Gee.HashMap<Application.BindingName, string>();
private Gee.Map<string, string> currentBindings = new Gee.HashMap<string, string>();

private Application(string[] args){
Application.singleton_instance = this;

Gtk.init(ref args);

Intl.bindtextdomain( Config.GETTEXT_PACKAGE, Config.LOCALEDIR );
Expand All @@ -29,19 +26,21 @@ namespace Gablabel

public int run(){
try{
keyBindings = new KeyBindingManager();

if(Config.USE_GSETTINGS == 1){
settings = new GLib.Settings("es.nohal.Gablabel");
on_settings_changed("clipboard-binding");
on_settings_changed("selected-binding");
settings.changed.connect(on_settings_changed);
}

window = new Gablabel.MainWindow();
statusIcon = new StatusIconManager(window);

window.start();
statusIcon.start();

keyBindings = new KeyBindingManager();
keyBindings.bind("<Ctrl><Alt>S", (event) => {
statusIcon.on_selected_activated();
});

settings = new GLib.Settings("es.nohal.gablabel");
settings.changed

Gtk.main();
return 0;
} catch(Error e){
Expand All @@ -50,8 +49,36 @@ namespace Gablabel
}
}

private void refreshBindings(){

private KeyBindingManager.KeybindingHandlerFunc? handler_func_by_setting(string key)
{
switch(key)
{
case "clipboard-binding":
return () => {
statusIcon.on_clipboard_activated();
};
case "selected-binding":
return () => {
statusIcon.on_selected_activated();
};
default:
return null;
}
}

private void on_settings_changed(string key)
{
switch(key)
{
case "clipboard-binding":
case "selected-binding":
if(currentBindings.has_key(key)){
keyBindings.unbind(currentBindings[key]);
}
currentBindings[key] = settings.get_string(key);
keyBindings.bind(currentBindings[key], handler_func_by_setting(key));
break;
}
}

public static int main(string[] args){
Expand Down
4 changes: 2 additions & 2 deletions src/MainWindow.vala
Expand Up @@ -27,8 +27,6 @@ namespace Gablabel
mainMenu = builder.get_object("main_menu") as Menu;
buttonReload = builder.get_object("toolbutton_reload") as ToolButton;
menuItemReload = builder.get_object("main_menu_reload") as ImageMenuItem;
AccelMap.add_entry("<Gablabel>/Clipboard", Gdk.keyval_from_name("r"), Gdk.ModifierType.CONTROL_MASK);
menuItemReload.set_accel_path("<Gablabel>/Clipboard");

//Signals connection
this.delete_event.connect(this.hide_on_delete);
Expand All @@ -41,6 +39,7 @@ namespace Gablabel
buttonMainMenu.set_menu(mainMenu);
buttonMainMenu.clicked.connect(() => { buttonMainMenu.show_menu(); });
buttonReload.clicked.connect(webView.load_translator);
(builder.get_object("toolbutton_preferences") as ToolButton).clicked.connect(show_preferences_dialog);

(builder.get_object("main_menu_fullscreen") as ImageMenuItem).activate.connect(() => {
if(this.isFullscreen){
Expand Down Expand Up @@ -97,6 +96,7 @@ namespace Gablabel
dialog.authors = authors;

dialog.run();
dialog.destroy();
}

public void show_preferences_dialog(){
Expand Down
32 changes: 27 additions & 5 deletions src/PreferencesDialogManager.vala
Expand Up @@ -4,12 +4,14 @@ namespace Gablabel
{
public class PreferencesDialogManager
{
private GLib.Settings settings;
private Dialog dialog;
private MainWindow parent;

public PreferencesDialogManager(MainWindow parent){
this.parent = parent;
}

public void run(){
var builder = new Builder();

try{
Expand All @@ -21,15 +23,35 @@ namespace Gablabel
ButtonsType.CLOSE,
_("Cannot launch de preferences dialog: %s"), e.message);
messageDialog.run();
messageDialog.destroy();
return;
}

dialog = builder.get_object("preferences_dialog") as Dialog;
settings = App().get_settings();
}

public void run(){
var settings = App().settings;

var clipboardEntry = builder.get_object("entry_clipboard") as Entry;
var selectedEntry = builder.get_object("entry_selected") as Entry;

settings.bind("clipboard-binding", clipboardEntry, "text", SettingsBindFlags.DEFAULT);
settings.bind("selected-binding", selectedEntry, "text", SettingsBindFlags.DEFAULT);

/*clipboardEntry.text = settings.get_string("clipboard-binding");
selectedEntry.text = settings.get_string("selected-binding");
clipboardEntry.focus_out_event.connect(() => {
stdout.printf("Se va a settear\n");
settings.set_string("clipboard-binding", clipboardEntry.text);
stdout.printf("Setteado\n");
return false;
});
selectedEntry.focus_out_event.connect((event) => {
settings.set_string("selected-binding", selectedEntry.text);
return false;
});*/

dialog.run();
dialog.destroy();
}
}
}
1 change: 0 additions & 1 deletion src/TranslatorWebView.vala
Expand Up @@ -55,7 +55,6 @@ namespace Gablabel
string finalText;
finalText = text.replace("\t", " ").escape("");
script = script.replace("@SOURCE_TEXT@", finalText);
stdout.printf(script + "\n");
this.execute_script(script);
} catch(Error e){
}
Expand Down
2 changes: 2 additions & 0 deletions vapi/config.vapi
Expand Up @@ -12,4 +12,6 @@ namespace Config {
* passed to underlying C code as cmd line macros. */
public const string LOCALEDIR; /* /usr/local/share/locale */
public const string DATADIR; /* /usr/local/share/gablabel */

public const int USE_GSETTINGS;
}

0 comments on commit a4b7b78

Please sign in to comment.