Skip to content

Commit

Permalink
Fix #24 Fix #13
Browse files Browse the repository at this point in the history
  • Loading branch information
Grabli66 committed Mar 14, 2018
1 parent 22a38a7 commit acb2f62
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 16 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ link_directories(${DEPS_LIBRARY_DIRS})

vala_precompile(VALA_C ${EXEC_NAME}
src/Main.vala
src/utils/TranslatorError.vala
src/LangInfo.vala
src/widgets/PopoverCombo.vala
src/TranslatorApplication.vala
Expand Down
32 changes: 29 additions & 3 deletions src/AsyncTaskExecuter.vala
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/// For executing task on thread
private class AsyncTask : GLib.Object {
private bool _isActive;
private int _timeout;
private AsyncTaskExecuter _parent;

/// Constructor
public AsyncTask(AsyncTaskExecuter parent, int timeout) {
_parent = parent;
_timeout = timeout;
}

/// Execute on thread
public void Run() {
try {
Thread.usleep(_timeout);
Expand All @@ -23,8 +26,12 @@ private class AsyncTask : GLib.Object {
});

_isActive = false;
} catch (Error e) {
stderr.printf(e.message);
}
catch(TranslatorError e) {
_parent.OnError(e);
}
catch (Error e) {
stderr.printf(e.message);
}
}

Expand All @@ -38,20 +45,39 @@ private class AsyncTask : GLib.Object {
}
}

/// Executer of AsyncTask
public class AsyncTaskExecuter : GLib.Object {
/// Thread pool
private ThreadPool<AsyncTask> _pool;

/// Async task to execute
private AsyncTask _task;

/// Timeout of execution
protected int ExecuteTimeout = 300000; // Timeout before executing

public virtual void OnExecute() {}
/// On error signal
public signal void error(TranslatorError error);

/// Main working method
public virtual void OnExecute() throws TranslatorError {}

/// On work result
public virtual void OnResult() {}

/// On error
public void OnError(TranslatorError err) {
error(err);
}

/// Constructor
public AsyncTaskExecuter() {
_pool = new ThreadPool<AsyncTask>.with_owned_data ((worker) => {
worker.Start ();
}, 7, false);
}

/// Run task
protected void Run() {
if (_task != null) {
_task.Stop();
Expand Down
9 changes: 7 additions & 2 deletions src/TranslatorService.vala
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ public class TranslateService : AsyncTaskExecuter {
private string _to;
private string _text;

/// On result
public signal void result(string[] text);

/// Constructor
public TranslateService() {
base();
}

public override void OnExecute() {
var ntext = Soup.URI.encode(_text, null);
/// Task main working method
public override void OnExecute() throws TranslatorError {
var ntext = Soup.URI.encode(_text, null);
var request = @"https://translate.yandex.net/api/v1.5/tr.json/translate?key=$(API_KEY)&lang=$(_from)-$(_to)&text=$(ntext)";
var root = WebJsonClient.Get(request);
var data = new Gee.ArrayList<string>();
Expand All @@ -31,10 +34,12 @@ public class TranslateService : AsyncTaskExecuter {
}
}

/// On result
public override void OnResult() {
result(_result);
}

/// Start to translate
public void Translate(string from, string to, string text) {
_from = from;
_to = to;
Expand Down
41 changes: 33 additions & 8 deletions src/TranslatorWindow.vala
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

// Main translator window
public class TranslateWindow : Gtk.ApplicationWindow {
private TranslateService service;
/// Service for translating
private TranslateService _translateService;
/// Dictionary service
private DictionaryService _dictService;

private GlobalSettings global = GlobalSettings.instance();
Expand Down Expand Up @@ -41,6 +43,9 @@ public class TranslateWindow : Gtk.ApplicationWindow {
private Gtk.TextTag _normalTag;
private Gtk.Label _dictLangLabel;

/// Toast for messages
private Granite.Widgets.Toast _toast;

private static int DEFAULT_WIDTH = 0;
private static int DEFAULT_HEIGHT = 640;

Expand Down Expand Up @@ -84,16 +89,27 @@ public class TranslateWindow : Gtk.ApplicationWindow {
Granite.Widgets.Utils.set_theming_for_screen (this.get_screen (), style, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
}

/// On service error
private void onServiceError(TranslatorError error) {
_isTranslating = false;
_progressSpinner.active = false;

_toast.title = _(error.message);
_toast.send_notification();
}

// Constructor
// TODO: separate to methods
public TranslateWindow() {
langs = global.getLangs();

service = new TranslateService();
service.result.connect(onTranslate);
_translateService = new TranslateService();
_translateService.result.connect(onTranslate);
_translateService.error.connect(onServiceError);

_dictService = new DictionaryService();
_dictService.result.connect(onDictResult);
_dictService.error.connect(onServiceError);

this.window_position = Gtk.WindowPosition.CENTER;
this.set_gravity(Gdk.Gravity.CENTER);
Expand Down Expand Up @@ -183,6 +199,8 @@ public class TranslateWindow : Gtk.ApplicationWindow {
var paned = new Gtk.Paned(Gtk.Orientation.VERTICAL);
_leftBox.pack_start(paned);

var topOverlay = new Gtk.Overlay();

topText = new Gtk.TextView();
topText.set_margin_left(7);
topText.set_margin_top(7);
Expand All @@ -194,6 +212,10 @@ public class TranslateWindow : Gtk.ApplicationWindow {
topScroll.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
topScroll.add (topText);

_toast = new Granite.Widgets.Toast("");
topOverlay.add(topScroll);
topOverlay.add_overlay(_toast);

/// Translate destination
var bottomOverlay = new Gtk.Overlay();

Expand All @@ -213,9 +235,10 @@ public class TranslateWindow : Gtk.ApplicationWindow {
_progressSpinner = new Gtk.Spinner ();
_progressSpinner.active = false;
_progressSpinner.margin = 70;



bottomOverlay.add(bottomScroll);
bottomOverlay.add_overlay(_progressSpinner);
bottomOverlay.add_overlay(_progressSpinner);

var topBox = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
var topLabelBox = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
Expand All @@ -229,7 +252,7 @@ public class TranslateWindow : Gtk.ApplicationWindow {
topLabelBox.pack_start(topLabelLang, false, true, 5);
topLabelBox.pack_end(topLabelLen, false, true, 5);

topBox.pack_start(topScroll);
topBox.pack_start(topOverlay);
topBox.pack_start(topLabelBox, false, true, 0);

var bottomBox = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
Expand Down Expand Up @@ -378,12 +401,14 @@ public class TranslateWindow : Gtk.ApplicationWindow {
}
}

/// On change value in left combobox
private void onLeftComboChange() {
onLangChange(false);
}

/// On change value in right combobox
private void onRightComboChange(LangInfo info) {
onLangChange(true);
onLangChange(true);
}

// Swap languages
Expand Down Expand Up @@ -413,7 +438,7 @@ public class TranslateWindow : Gtk.ApplicationWindow {

_isTranslating = true;
_progressSpinner.active = true;
service.Translate(leftLang.id, rightLang.id, topText.buffer.text);
_translateService.Translate(leftLang.id, rightLang.id, topText.buffer.text);
}

/// On translate complete
Expand Down
15 changes: 12 additions & 3 deletions src/WebJsonClient.vala
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
/// For fetch json from url
public class WebJsonClient : GLib.Object {
private static Json.Parser _parser;
/// No connection to server code
private const int NO_CONNECTION = 2;

/// Json parser
private static Json.Parser _parser;
private static Json.Parser parser {
get {
if (_parser == null) _parser = new Json.Parser();
return _parser;
}
}

public static Json.Object Get(string request) {
/// Get json from url
public static Json.Object Get(string request) throws TranslatorError {
var session = new Soup.SessionSync ();
session.timeout = GlobalSettings.SERVER_RESPOND_TIMEOUT;
session.proxy_uri = GlobalSettings.getProxyUri();

var url = new Soup.URI(request);
var message = new Soup.Message.from_uri ("GET", url);

var status = session.send_message (message);
var status = session.send_message(message);
if (status == NO_CONNECTION) {
throw new TranslatorError.NoConnection(_("No connection to server"));
}

var mess = (string)message.response_body.data;
parser.load_from_data (mess);
return parser.get_root ().get_object ();
Expand Down
5 changes: 5 additions & 0 deletions src/utils/TranslatorError.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// Exceptions
public errordomain TranslatorError {
/// No connection error
NoConnection;
}

0 comments on commit acb2f62

Please sign in to comment.