summaryrefslogtreecommitdiff
path: root/src/simple-scan.vala
diff options
context:
space:
mode:
Diffstat (limited to 'src/simple-scan.vala')
-rw-r--r--src/simple-scan.vala175
1 files changed, 70 insertions, 105 deletions
diff --git a/src/simple-scan.vala b/src/simple-scan.vala
index 2ab83f0..841e702 100644
--- a/src/simple-scan.vala
+++ b/src/simple-scan.vala
@@ -23,7 +23,7 @@ public class SimpleScan : Gtk.Application
/* Help string for command line --debug flag */
N_("Print debugging messages"), null},
{ "fix-pdf", 0, 0, OptionArg.STRING, ref fix_pdf_filename,
- N_("Fix PDF files generated with older versions of Simple Scan"), "FILENAME..."},
+ N_("Fix PDF files generated with older versions of Simple Scan"), "FILENAME…"},
{ null }
};
private static Timer log_timer;
@@ -32,12 +32,16 @@ public class SimpleScan : Gtk.Application
private ScanDevice? default_device = null;
private bool have_devices = false;
private GUsb.Context usb_context;
- private UserInterface ui;
+ private AppWindow app;
private Scanner scanner;
private Book book;
public SimpleScan (ScanDevice? device = null)
{
+ /* The inhibit () method use this */
+ Object (application_id: "org.gnome.SimpleScan");
+ register_session = true;
+
default_device = device;
}
@@ -45,11 +49,10 @@ public class SimpleScan : Gtk.Application
{
base.startup ();
- ui = new UserInterface ();
- book = ui.book;
- ui.start_scan.connect (scan_cb);
- ui.stop_scan.connect (cancel_cb);
- ui.email.connect (email_cb);
+ app = new AppWindow ();
+ book = app.book;
+ app.start_scan.connect (scan_cb);
+ app.stop_scan.connect (cancel_cb);
scanner = Scanner.get_instance ();
scanner.update_devices.connect (update_scan_devices_cb);
@@ -78,15 +81,15 @@ public class SimpleScan : Gtk.Application
List<ScanDevice> device_list = null;
device_list.append (default_device);
- ui.set_scan_devices (device_list);
- ui.selected_device = default_device.name;
+ app.set_scan_devices (device_list);
+ app.selected_device = default_device.name;
}
}
public override void activate ()
{
base.activate ();
- ui.start ();
+ app.start ();
scanner.start ();
}
@@ -94,7 +97,7 @@ public class SimpleScan : Gtk.Application
{
base.shutdown ();
book = null;
- ui = null;
+ app = null;
usb_context = null;
scanner.free ();
}
@@ -127,7 +130,7 @@ public class SimpleScan : Gtk.Application
if (!have_devices)
missing_driver = suggest_driver ();
- ui.set_scan_devices (devices_copy, missing_driver);
+ app.set_scan_devices (devices_copy, missing_driver);
}
/* Taken from /usr/local/Brother/sane/Brsane.ini from brscan driver */
@@ -212,7 +215,7 @@ public class SimpleScan : Gtk.Application
private void authorize_cb (Scanner scanner, string resource)
{
string username, password;
- ui.authorize (resource, out username, out password);
+ app.authorize (resource, out username, out password);
scanner.authorize (username, password);
}
@@ -222,7 +225,7 @@ public class SimpleScan : Gtk.Application
var page = book.get_page (-1);
if (page != null && !page.has_data)
{
- ui.selected_page = page;
+ app.selected_page = page;
page.start ();
return page;
}
@@ -262,7 +265,7 @@ public class SimpleScan : Gtk.Application
page.set_custom_crop (cw, ch);
page.move_crop (cx, cy);
}
- ui.selected_page = page;
+ app.selected_page = page;
page.start ();
return page;
@@ -385,118 +388,80 @@ public class SimpleScan : Gtk.Application
remove_empty_page ();
if (error_code != Sane.Status.CANCELLED)
{
- ui.show_error (/* Title of error dialog when scan failed */
- _("Failed to scan"),
- error_string,
- have_devices);
+ app.show_error_dialog (/* Title of error dialog when scan failed */
+ _("Failed to scan"),
+ error_string);
}
}
- private void scanner_scanning_changed_cb (Scanner scanner)
- {
- ui.scanning = scanner.is_scanning ();
- }
-
- private void scan_cb (UserInterface ui, string? device, ScanOptions options)
- {
- debug ("Requesting scan at %d dpi from device '%s'", options.dpi, device);
+ private uint inhibit_cookie;
+ private FreedesktopScreensaver? fdss;
- if (!scanner.is_scanning ())
- append_page ();
-
- scanner.scan (device, options);
- }
-
- private void cancel_cb (UserInterface ui)
- {
- scanner.cancel ();
- }
-
- private string? get_temporary_filename (string prefix, string extension)
+ private void scanner_scanning_changed_cb (Scanner scanner)
{
- /* NOTE: I'm not sure if this is a 100% safe strategy to use g_file_open_tmp(), close and
- * use the filename but it appears to work in practise */
+ var is_scanning = scanner.is_scanning ();
- var filename = "%sXXXXXX.%s".printf (prefix, extension);
- string path;
- try
- {
- var fd = FileUtils.open_tmp (filename, out path);
- Posix.close (fd);
- }
- catch (Error e)
+ if (is_scanning)
{
- warning ("Error saving email attachment: %s", e.message);
- return null;
- }
+ /* Attempt to inhibit the screensaver when scanning */
+ var reason = _("Scan in progress");
- return path;
- }
+ /* This should work on Gnome, Budgie, Cinnamon, Mate, Unity, ...
+ * but will not work on KDE, LXDE, XFCE, ... */
+ inhibit_cookie = inhibit (app, Gtk.ApplicationInhibitFlags.IDLE, reason);
- private void email_cb (UserInterface ui, string profile, int quality)
- {
- var saved = false;
- var command_line = "xdg-email";
-
- /* Save text files as PDFs */
- if (profile == "text")
- {
- /* Open a temporary file */
- var path = get_temporary_filename ("scan", "pdf");
- if (path != null)
+ if (!is_inhibited (Gtk.ApplicationInhibitFlags.IDLE))
{
- var file = File.new_for_path (path);
- ui.show_progress_dialog ();
+ /* If the previous method didn't work, try the one
+ * provided by Freedesktop. It should work with KDE,
+ * LXDE, XFCE, and maybe others as well. */
try
{
- book.save ("pdf", quality, file);
+ if ((fdss = FreedesktopScreensaver.get_proxy ()) != null)
+ {
+ inhibit_cookie = fdss.inhibit ("Simple-Scan", reason);
+ }
}
- catch (Error e)
- {
- ui.hide_progress_dialog ();
- warning ("Unable to save email file: %s", e.message);
- return;
- }
- command_line += " --attach %s".printf (path);
+ catch (IOError error) {}
}
}
else
{
- for (var i = 0; i < book.n_pages; i++)
+ /* When finished scanning, uninhibit if inhibit was working */
+ if (inhibit_cookie != 0)
{
- var path = get_temporary_filename ("scan", "jpg");
- if (path == null)
- {
- saved = false;
- break;
- }
-
- var file = File.new_for_path (path);
- try
- {
- book.get_page (i).save ("jpeg", quality, file);
- }
- catch (Error e)
+ if (fdss == null)
+ uninhibit (inhibit_cookie);
+ else
{
- warning ("Unable to save email file: %s", e.message);
- return;
+ try
+ {
+ fdss.uninhibit (inhibit_cookie);
+ }
+ catch (IOError error) {}
+ fdss = null;
}
- command_line += " --attach %s".printf (path);
- if (!saved)
- break;
+ inhibit_cookie = 0;
}
}
- debug ("Launching email client: %s", command_line);
- try
- {
- Process.spawn_command_line_async (command_line);
- }
- catch (Error e)
- {
- warning ("Unable to start email: %s", e.message);
- }
+ app.scanning = is_scanning;
+ }
+
+ private void scan_cb (AppWindow ui, string? device, ScanOptions options)
+ {
+ debug ("Requesting scan at %d dpi from device '%s'", options.dpi, device);
+
+ if (!scanner.is_scanning ())
+ append_page ();
+
+ scanner.scan (device, options);
+ }
+
+ private void cancel_cb (AppWindow ui)
+ {
+ scanner.cancel ();
}
private static void log_cb (string? log_domain, LogLevelFlags log_level, string message)
@@ -616,7 +581,7 @@ public class SimpleScan : Gtk.Application
Intl.textdomain (GETTEXT_PACKAGE);
var c = new OptionContext (/* Arguments and description for --help text */
- _("[DEVICE...] - Scanning utility"));
+ _("[DEVICE…] — Scanning utility"));
c.add_main_entries (options, GETTEXT_PACKAGE);
c.add_group (Gtk.get_option_group (true));
try
@@ -627,7 +592,7 @@ public class SimpleScan : Gtk.Application
{
stderr.printf ("%s\n", e.message);
stderr.printf (/* Text printed out when an unknown command-line argument provided */
- _("Run '%s --help' to see a full list of available command line options."), args[0]);
+ _("Run “%s --help” to see a full list of available command line options."), args[0]);
stderr.printf ("\n");
return Posix.EXIT_FAILURE;
}