summaryrefslogtreecommitdiff
path: root/src/simple-scan.c
diff options
context:
space:
mode:
authorAlessio Treglia <alessio@debian.org>2011-09-03 12:12:52 +0200
committerAlessio Treglia <alessio@debian.org>2011-09-03 12:12:52 +0200
commit8227fe5bd1a0cee50359c2581184c32a371c754e (patch)
tree80723467abe036465fd977816881849a70030111 /src/simple-scan.c
parent87ebc2af1f0417b3bc38a233e28ff673eff4fa51 (diff)
Imported Upstream version 3.1.90upstream/3.1.90
Diffstat (limited to 'src/simple-scan.c')
-rw-r--r--src/simple-scan.c374
1 files changed, 334 insertions, 40 deletions
diff --git a/src/simple-scan.c b/src/simple-scan.c
index 1f1f599..85943bf 100644
--- a/src/simple-scan.c
+++ b/src/simple-scan.c
@@ -15,9 +15,9 @@
#include <glib.h>
#include <glib-object.h>
#include <gudev/gudev.h>
-#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <stdio.h>
#include <float.h>
#include <math.h>
#include <sane/sane.h>
@@ -140,9 +140,12 @@ typedef struct _ScanOptionsPrivate ScanOptionsPrivate;
#define TYPE_SCAN_TYPE (scan_type_get_type ())
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
+#define _fclose0(var) ((var == NULL) ? NULL : (var = (fclose (var), NULL)))
+#define _g_match_info_free0(var) ((var == NULL) ? NULL : (var = (g_match_info_free (var), NULL)))
+#define _g_string_free0(var) ((var == NULL) ? NULL : (var = (g_string_free (var, TRUE), NULL)))
+#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))
#define _g_option_context_free0(var) ((var == NULL) ? NULL : (var = (g_option_context_free (var), NULL)))
#define _g_timer_destroy0(var) ((var == NULL) ? NULL : (var = (g_timer_destroy (var), NULL)))
-#define _fclose0(var) ((var == NULL) ? NULL : (var = (fclose (var), NULL)))
#define _application_unref0(var) ((var == NULL) ? NULL : (var = (application_unref (var), NULL)))
typedef struct _ParamSpecApplication ParamSpecApplication;
@@ -244,6 +247,8 @@ static gboolean application_show_version;
static gboolean application_show_version = FALSE;
static gboolean application_debug_enabled;
static gboolean application_debug_enabled = FALSE;
+static gchar* application_fix_pdf_filename;
+static gchar* application_fix_pdf_filename = NULL;
static GTimer* application_log_timer;
static GTimer* application_log_timer = NULL;
static FILE* application_log_file;
@@ -398,6 +403,7 @@ void page_save (Page* self, const gchar* type, GFile* file, GError** error);
void scanner_free (Scanner* self);
static void application_log_cb (const gchar* log_domain, GLogLevelFlags log_level, const gchar* message);
void scanner_redetect (Scanner* self);
+static void application_fix_pdf (const gchar* filename, GError** error);
gint application_main (gchar** args, int args_length1);
ScanDevice* scan_device_new (void);
ScanDevice* scan_device_construct (GType object_type);
@@ -406,7 +412,7 @@ static void application_finalize (Application* obj);
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func);
-const GOptionEntry APPLICATION_options[3] = {{"version", 'v', 0, G_OPTION_ARG_NONE, &application_show_version, "Show release version", NULL}, {"debug", 'd', 0, G_OPTION_ARG_NONE, &application_debug_enabled, "Print debugging messages", NULL}, {NULL}};
+const GOptionEntry APPLICATION_options[4] = {{"version", 'v', 0, G_OPTION_ARG_NONE, &application_show_version, "Show release version", NULL}, {"debug", 'd', 0, G_OPTION_ARG_NONE, &application_debug_enabled, "Print debugging messages", NULL}, {"fix-pdf", (gchar) 0, 0, G_OPTION_ARG_STRING, &application_fix_pdf_filename, "Fix PDF files generated with older versions of Simple Scan", "FILENAME..."}, {NULL}};
static gpointer _scan_device_ref0 (gpointer self) {
return self ? scan_device_ref (self) : NULL;
@@ -747,7 +753,7 @@ static void application_scanner_page_info_cb (Application* self, Scanner* scanne
g_return_if_fail (self != NULL);
g_return_if_fail (scanner != NULL);
g_return_if_fail (info != NULL);
- g_debug ("simple-scan.vala:233: Page is %d pixels wide, %d pixels high, %d bits " \
+ g_debug ("simple-scan.vala:236: Page is %d pixels wide, %d pixels high, %d bits " \
"per pixel", info->width, info->height, info->depth);
_tmp0_ = application_append_page (self);
page = _tmp0_;
@@ -848,7 +854,7 @@ static void application_scan_cb (Application* self, SimpleScan* ui, const gchar*
g_return_if_fail (self != NULL);
g_return_if_fail (ui != NULL);
g_return_if_fail (options != NULL);
- g_debug ("simple-scan.vala:293: Requesting scan at %d dpi from device '%s'", options->dpi, device);
+ g_debug ("simple-scan.vala:296: Requesting scan at %d dpi from device '%s'", options->dpi, device);
_tmp0_ = scanner_is_scanning (self->priv->scanner);
if (!_tmp0_) {
Page* _tmp1_ = NULL;
@@ -918,7 +924,7 @@ static gchar* application_get_temporary_filename (Application* self, const gchar
GError* e = NULL;
e = _inner_error_;
_inner_error_ = NULL;
- g_warning ("simple-scan.vala:329: Error saving email attachment: %s", e->message);
+ g_warning ("simple-scan.vala:332: Error saving email attachment: %s", e->message);
result = NULL;
_g_error_free0 (e);
_g_free0 (path);
@@ -975,7 +981,7 @@ static void application_email_cb (Application* self, SimpleScan* ui, const gchar
GError* e = NULL;
e = _inner_error_;
_inner_error_ = NULL;
- g_warning ("simple-scan.vala:355: Unable to save email file: %s", e->message);
+ g_warning ("simple-scan.vala:358: Unable to save email file: %s", e->message);
_g_error_free0 (e);
_g_object_unref0 (file);
_g_free0 (path);
@@ -1050,7 +1056,7 @@ static void application_email_cb (Application* self, SimpleScan* ui, const gchar
GError* e = NULL;
e = _inner_error_;
_inner_error_ = NULL;
- g_warning ("simple-scan.vala:379: Unable to save email file: %s", e->message);
+ g_warning ("simple-scan.vala:382: Unable to save email file: %s", e->message);
_g_error_free0 (e);
_g_object_unref0 (file);
_g_free0 (path);
@@ -1083,7 +1089,7 @@ static void application_email_cb (Application* self, SimpleScan* ui, const gchar
}
}
}
- g_debug ("simple-scan.vala:389: Launching email client: %s", command_line);
+ g_debug ("simple-scan.vala:392: Launching email client: %s", command_line);
{
g_spawn_command_line_async (command_line, &_inner_error_);
if (_inner_error_ != NULL) {
@@ -1096,7 +1102,7 @@ static void application_email_cb (Application* self, SimpleScan* ui, const gchar
GError* e = NULL;
e = _inner_error_;
_inner_error_ = NULL;
- g_warning ("simple-scan.vala:396: Unable to start email: %s", e->message);
+ g_warning ("simple-scan.vala:399: Unable to start email: %s", e->message);
_g_error_free0 (e);
}
__finally6:
@@ -1209,6 +1215,243 @@ static void application_on_uevent (Application* self, GUdevClient* client, const
}
+static glong string_strnlen (gchar* str, glong maxlen) {
+ glong result = 0L;
+ gchar* _tmp0_ = NULL;
+ gchar* end;
+ _tmp0_ = memchr (str, 0, (gsize) maxlen);
+ end = _tmp0_;
+ if (end == NULL) {
+ result = maxlen;
+ return result;
+ } else {
+ result = (glong) (end - str);
+ return result;
+ }
+}
+
+
+static gchar* string_substring (const gchar* self, glong offset, glong len) {
+ gchar* result = NULL;
+ glong string_length = 0L;
+ gboolean _tmp0_ = FALSE;
+ gchar* _tmp3_ = NULL;
+ g_return_val_if_fail (self != NULL, NULL);
+ if (offset >= ((glong) 0)) {
+ _tmp0_ = len >= ((glong) 0);
+ } else {
+ _tmp0_ = FALSE;
+ }
+ if (_tmp0_) {
+ glong _tmp1_;
+ _tmp1_ = string_strnlen ((gchar*) self, offset + len);
+ string_length = _tmp1_;
+ } else {
+ gint _tmp2_;
+ _tmp2_ = strlen (self);
+ string_length = (glong) _tmp2_;
+ }
+ if (offset < ((glong) 0)) {
+ offset = string_length + offset;
+ g_return_val_if_fail (offset >= ((glong) 0), NULL);
+ } else {
+ g_return_val_if_fail (offset <= string_length, NULL);
+ }
+ if (len < ((glong) 0)) {
+ len = string_length - offset;
+ }
+ g_return_val_if_fail ((offset + len) <= string_length, NULL);
+ _tmp3_ = g_strndup (((gchar*) self) + offset, (gsize) len);
+ result = _tmp3_;
+ return result;
+}
+
+
+static void application_fix_pdf (const gchar* filename, GError** error) {
+ guint8* data = NULL;
+ gint data_length1 = 0;
+ gint _data_size_ = 0;
+ guint8* _tmp0_ = NULL;
+ size_t _tmp1_;
+ gchar* _tmp2_;
+ gchar* _tmp3_;
+ FILE* _tmp4_ = NULL;
+ FILE* _tmp5_;
+ FILE* fixed_file;
+ gint offset;
+ gint line_number;
+ gint xref_offset;
+ gint xref_line;
+ GRegex* _tmp6_ = NULL;
+ GRegex* xref_regex;
+ GMatchInfo* xref_match = NULL;
+ GString* _tmp7_ = NULL;
+ GString* line;
+ gchar* _tmp25_;
+ gchar* _tmp26_;
+ gint _tmp27_;
+ gboolean _tmp28_;
+ GError * _inner_error_ = NULL;
+ g_return_if_fail (filename != NULL);
+ g_file_get_contents (filename, (gchar**) (&_tmp0_), &_tmp1_, &_inner_error_);
+ data = (g_free (data), NULL);
+ data = _tmp0_;
+ data_length1 = _tmp1_;
+ _data_size_ = data_length1;
+ if (_inner_error_ != NULL) {
+ g_propagate_error (error, _inner_error_);
+ data = (g_free (data), NULL);
+ return;
+ }
+ _tmp2_ = g_strconcat (filename, ".fixed", NULL);
+ _tmp3_ = _tmp2_;
+ _tmp4_ = fopen (_tmp3_, "w");
+ _tmp5_ = _tmp4_;
+ _g_free0 (_tmp3_);
+ fixed_file = _tmp5_;
+ offset = 0;
+ line_number = 0;
+ xref_offset = 0;
+ xref_line = -1;
+ _tmp6_ = g_regex_new ("^\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d 0000 n$", 0, 0, &_inner_error_);
+ xref_regex = _tmp6_;
+ if (_inner_error_ != NULL) {
+ g_propagate_error (error, _inner_error_);
+ _fclose0 (fixed_file);
+ data = (g_free (data), NULL);
+ return;
+ }
+ _tmp7_ = g_string_new ("");
+ line = _tmp7_;
+ while (TRUE) {
+ gint end_offset;
+ gboolean _tmp8_ = FALSE;
+ if (!(offset < data_length1)) {
+ break;
+ }
+ end_offset = offset;
+ g_string_assign (line, "");
+ while (TRUE) {
+ guint8 c;
+ if (!(end_offset < data_length1)) {
+ break;
+ }
+ c = data[end_offset];
+ g_string_append_c (line, (gchar) c);
+ end_offset++;
+ if (c == ((guint8) '\n')) {
+ break;
+ }
+ }
+ if (g_strcmp0 (line->str, "startxref\n") == 0) {
+ xref_line = line_number;
+ }
+ if (line_number < 2) {
+ gboolean _tmp9_;
+ _tmp9_ = g_str_has_prefix (line->str, "%%");
+ _tmp8_ = _tmp9_;
+ } else {
+ _tmp8_ = FALSE;
+ }
+ if (_tmp8_) {
+ gchar* _tmp10_ = NULL;
+ gchar* _tmp11_;
+ xref_offset--;
+ _tmp10_ = string_substring (line->str, (glong) 1, (glong) (-1));
+ _tmp11_ = _tmp10_;
+ fprintf (fixed_file, "%s", _tmp11_);
+ _g_free0 (_tmp11_);
+ } else {
+ GMatchInfo* _tmp12_ = NULL;
+ gboolean _tmp13_;
+ _tmp13_ = g_regex_match (xref_regex, line->str, 0, &_tmp12_);
+ _g_match_info_free0 (xref_match);
+ xref_match = _tmp12_;
+ if (_tmp13_) {
+ const gchar* _tmp14_ = NULL;
+ gint _tmp15_;
+ _tmp14_ = g_match_info_get_string (xref_match);
+ _tmp15_ = atoi (_tmp14_);
+ fprintf (fixed_file, "%010d 00000 n \n", _tmp15_ + xref_offset);
+ } else {
+ gboolean _tmp16_ = FALSE;
+ if (xref_line > 0) {
+ _tmp16_ = line_number == (xref_line + 1);
+ } else {
+ _tmp16_ = FALSE;
+ }
+ if (_tmp16_) {
+ gint _tmp17_;
+ gchar* _tmp18_ = NULL;
+ gchar* _tmp19_;
+ _tmp17_ = atoi (line->str);
+ _tmp18_ = g_strdup_printf ("%d\n", _tmp17_ + xref_offset);
+ _tmp19_ = _tmp18_;
+ fprintf (fixed_file, "%s", _tmp19_);
+ _g_free0 (_tmp19_);
+ } else {
+ gboolean _tmp20_ = FALSE;
+ if (line_number == (xref_line + 2)) {
+ gboolean _tmp21_;
+ _tmp21_ = g_str_has_prefix (line->str, "%%%%");
+ _tmp20_ = _tmp21_;
+ } else {
+ _tmp20_ = FALSE;
+ }
+ if (_tmp20_) {
+ gchar* _tmp22_ = NULL;
+ gchar* _tmp23_;
+ _tmp22_ = string_substring (line->str, (glong) 2, (glong) (-1));
+ _tmp23_ = _tmp22_;
+ fprintf (fixed_file, "%s", _tmp23_);
+ _g_free0 (_tmp23_);
+ } else {
+ {
+ gint i;
+ i = offset;
+ {
+ gboolean _tmp24_;
+ _tmp24_ = TRUE;
+ while (TRUE) {
+ if (!_tmp24_) {
+ i++;
+ }
+ _tmp24_ = FALSE;
+ if (!(i < end_offset)) {
+ break;
+ }
+ fputc ((gchar) data[i], fixed_file);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ line_number++;
+ offset = end_offset;
+ }
+ _tmp25_ = g_strconcat (filename, "~", NULL);
+ _tmp26_ = _tmp25_;
+ _tmp27_ = g_rename (filename, _tmp26_);
+ _tmp28_ = _tmp27_ >= 0;
+ _g_free0 (_tmp26_);
+ if (_tmp28_) {
+ gchar* _tmp29_;
+ gchar* _tmp30_;
+ _tmp29_ = g_strconcat (filename, ".fixed", NULL);
+ _tmp30_ = _tmp29_;
+ g_rename (_tmp30_, filename);
+ _g_free0 (_tmp30_);
+ }
+ _g_string_free0 (line);
+ _g_match_info_free0 (xref_match);
+ _g_regex_unref0 (xref_regex);
+ _fclose0 (fixed_file);
+ data = (g_free (data), NULL);
+}
+
+
static void _application_log_cb_glog_func (const gchar* log_domain, GLogLevelFlags log_levels, const gchar* message, gpointer self) {
application_log_cb (log_domain, log_levels, message);
}
@@ -1221,15 +1464,15 @@ gint application_main (gchar** args, int args_length1) {
GOptionContext* c;
GOptionGroup* _tmp2_ = NULL;
ScanDevice* device;
- GTimer* _tmp7_ = NULL;
- const gchar* _tmp8_ = NULL;
- gchar* _tmp9_ = NULL;
+ GTimer* _tmp8_ = NULL;
+ const gchar* _tmp9_ = NULL;
+ gchar* _tmp10_ = NULL;
gchar* path;
- const gchar* _tmp10_ = NULL;
- gchar* _tmp11_ = NULL;
- FILE* _tmp12_ = NULL;
- pid_t _tmp13_;
- Application* _tmp14_ = NULL;
+ const gchar* _tmp11_ = NULL;
+ gchar* _tmp12_ = NULL;
+ FILE* _tmp13_ = NULL;
+ pid_t _tmp14_;
+ Application* _tmp15_ = NULL;
Application* app;
GError * _inner_error_ = NULL;
setlocale (LC_ALL, "");
@@ -1278,40 +1521,91 @@ gint application_main (gchar** args, int args_length1) {
_g_option_context_free0 (c);
return result;
}
+ if (application_fix_pdf_filename != NULL) {
+ {
+ application_fix_pdf (application_fix_pdf_filename, &_inner_error_);
+ if (_inner_error_ != NULL) {
+ goto __catch8_g_error;
+ }
+ {
+ gint i;
+ i = 1;
+ {
+ gboolean _tmp4_;
+ _tmp4_ = TRUE;
+ while (TRUE) {
+ if (!_tmp4_) {
+ i++;
+ }
+ _tmp4_ = FALSE;
+ if (!(i < args_length1)) {
+ break;
+ }
+ application_fix_pdf (args[i], &_inner_error_);
+ if (_inner_error_ != NULL) {
+ goto __catch8_g_error;
+ }
+ }
+ }
+ }
+ }
+ goto __finally8;
+ __catch8_g_error:
+ {
+ GError* e = NULL;
+ e = _inner_error_;
+ _inner_error_ = NULL;
+ fprintf (stderr, "Error fixing PDF file: %s", e->message);
+ result = EXIT_FAILURE;
+ _g_error_free0 (e);
+ _g_option_context_free0 (c);
+ return result;
+ }
+ __finally8:
+ if (_inner_error_ != NULL) {
+ _g_option_context_free0 (c);
+ g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
+ g_clear_error (&_inner_error_);
+ return 0;
+ }
+ result = EXIT_SUCCESS;
+ _g_option_context_free0 (c);
+ return result;
+ }
device = NULL;
if (args_length1 > 1) {
- ScanDevice* _tmp4_ = NULL;
- gchar* _tmp5_;
+ ScanDevice* _tmp5_ = NULL;
gchar* _tmp6_;
- _tmp4_ = scan_device_new ();
+ gchar* _tmp7_;
+ _tmp5_ = scan_device_new ();
_scan_device_unref0 (device);
- device = _tmp4_;
- _tmp5_ = g_strdup (args[1]);
- _g_free0 (device->name);
- device->name = _tmp5_;
+ device = _tmp5_;
_tmp6_ = g_strdup (args[1]);
+ _g_free0 (device->name);
+ device->name = _tmp6_;
+ _tmp7_ = g_strdup (args[1]);
_g_free0 (device->label);
- device->label = _tmp6_;
+ device->label = _tmp7_;
}
- _tmp7_ = g_timer_new ();
+ _tmp8_ = g_timer_new ();
_g_timer_destroy0 (application_log_timer);
- application_log_timer = _tmp7_;
- _tmp8_ = g_get_user_cache_dir ();
- _tmp9_ = g_build_filename (_tmp8_, "simple-scan", NULL, NULL);
- path = _tmp9_;
+ application_log_timer = _tmp8_;
+ _tmp9_ = g_get_user_cache_dir ();
+ _tmp10_ = g_build_filename (_tmp9_, "simple-scan", NULL, NULL);
+ path = _tmp10_;
g_mkdir_with_parents (path, 0700);
- _tmp10_ = g_get_user_cache_dir ();
- _tmp11_ = g_build_filename (_tmp10_, "simple-scan", "simple-scan.log", NULL, NULL);
+ _tmp11_ = g_get_user_cache_dir ();
+ _tmp12_ = g_build_filename (_tmp11_, "simple-scan", "simple-scan.log", NULL, NULL);
_g_free0 (path);
- path = _tmp11_;
- _tmp12_ = fopen (path, "w");
+ path = _tmp12_;
+ _tmp13_ = fopen (path, "w");
_fclose0 (application_log_file);
- application_log_file = _tmp12_;
+ application_log_file = _tmp13_;
g_log_set_default_handler (_application_log_cb_glog_func, NULL);
- _tmp13_ = getpid ();
- g_debug ("simple-scan.vala:507: Starting Simple Scan %s, PID=%i", VERSION, (gint) _tmp13_);
- _tmp14_ = application_new (device);
- app = _tmp14_;
+ _tmp14_ = getpid ();
+ g_debug ("simple-scan.vala:586: Starting Simple Scan %s, PID=%i", VERSION, (gint) _tmp14_);
+ _tmp15_ = application_new (device);
+ app = _tmp15_;
application_start (app);
gtk_main ();
result = EXIT_SUCCESS;