summaryrefslogtreecommitdiffstats
path: root/src/pdf-invoice
diff options
context:
space:
mode:
authorSebastian Reichel <sre@ring0.de>2013-03-26 15:52:57 +0100
committerSebastian Reichel <sre@ring0.de>2013-03-26 15:52:57 +0100
commit7bfb48ef84384ff0460f273ea5841fba628d2a46 (patch)
tree898d019f33a554f03cac91495adcb7165344382e /src/pdf-invoice
parent03a4e9f901cd36792de2172b4ebb8f6e852fe1cd (diff)
downloadserial-barcode-scanner-7bfb48ef84384ff0460f273ea5841fba628d2a46.tar.bz2
code restructure
Diffstat (limited to 'src/pdf-invoice')
-rw-r--r--src/pdf-invoice/Makefile9
-rw-r--r--src/pdf-invoice/main.vala36
-rw-r--r--src/pdf-invoice/pdf-invoice-interface.vala54
-rw-r--r--src/pdf-invoice/pdf-invoice.vala98
-rw-r--r--src/pdf-invoice/test.vala47
5 files changed, 169 insertions, 75 deletions
diff --git a/src/pdf-invoice/Makefile b/src/pdf-invoice/Makefile
index 2a5d2a4..cc6db94 100644
--- a/src/pdf-invoice/Makefile
+++ b/src/pdf-invoice/Makefile
@@ -1,9 +1,12 @@
all: pdf-invoice
-pdf-invoice: pdf-invoice.vala ../price.vapi
- valac --pkg pangocairo --pkg posix --pkg gio-2.0 $^
+pdf-invoice: main.vala pdf-invoice.vala pdf-invoice-interface.vala ../price.vapi
+ valac -g -o $@ --pkg pangocairo --pkg librsvg-2.0 --pkg posix --pkg gdk-2.0 --pkg gio-2.0 $^
+
+test: pdf-invoice-interface.vala test.vala ../price.vapi
+ valac -o $@ --pkg gio-2.0 $^
clean:
- rm -rf pdf-invoice
+ rm -rf pdf-invoice test
.PHONY: all clean
diff --git a/src/pdf-invoice/main.vala b/src/pdf-invoice/main.vala
new file mode 100644
index 0000000..76b03c6
--- /dev/null
+++ b/src/pdf-invoice/main.vala
@@ -0,0 +1,36 @@
+/* Copyright 2013, Sebastian Reichel <sre@ring0.de>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+public static int main(string[] args) {
+ Bus.own_name(
+ BusType.SESSION,
+ "io.mainframe.shopsystem.InvoicePDF",
+ BusNameOwnerFlags.NONE,
+ on_bus_aquired,
+ () => {},
+ () => stderr.printf("Could not aquire name\n"));
+
+ new MainLoop().run();
+
+ return 0;
+}
+
+void on_bus_aquired(DBusConnection conn) {
+ try {
+ conn.register_object("/io/mainframe/shopsystem/invoicepdf", new InvoicePDF());
+ } catch(IOError e) {
+ stderr.printf("Could not register service\n");
+ }
+}
diff --git a/src/pdf-invoice/pdf-invoice-interface.vala b/src/pdf-invoice/pdf-invoice-interface.vala
new file mode 100644
index 0000000..d2c04d9
--- /dev/null
+++ b/src/pdf-invoice/pdf-invoice-interface.vala
@@ -0,0 +1,54 @@
+/* Copyright 2013, Sebastian Reichel <sre@ring0.de>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+[DBus (name = "io.mainframe.shopsystem.InvoicePDFError")]
+public errordomain InvoicePDFError {
+ /* missing invoice data */
+ NO_INVOICE_DATA,
+ NO_INVOICE_DATE,
+ NO_INVOICE_ID,
+ NO_INVOICE_RECIPIENT,
+
+ /* data not supported by renderer */
+ ARTICLE_NAME_TOO_LONG,
+ PRICE_TOO_HIGH,
+ TOO_FAR_IN_THE_FUTURE
+}
+
+public struct InvoiceRecipient {
+ public string firstname;
+ public string lastname;
+ public string street;
+ public string postal_code;
+ public string city;
+ public string gender;
+}
+
+public struct InvoiceEntry {
+ int timestamp;
+ string article;
+ Price price;
+}
+
+[DBus (name = "io.mainframe.shopsystem.InvoicePDF")]
+public interface PDFInvoice : Object {
+ public abstract string invoice_id { set; owned get; }
+ public abstract int64 invoice_date { set; owned get; }
+ public abstract InvoiceRecipient invoice_recipient { set; owned get; }
+ public abstract InvoiceEntry[] invoice_entries { set; owned get; }
+
+ public abstract uint8[] generate() throws IOError, InvoicePDFError;
+ public abstract void clear() throws IOError;
+}
diff --git a/src/pdf-invoice/pdf-invoice.vala b/src/pdf-invoice/pdf-invoice.vala
index 92598f6..095dcc6 100644
--- a/src/pdf-invoice/pdf-invoice.vala
+++ b/src/pdf-invoice/pdf-invoice.vala
@@ -13,35 +13,6 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-[DBus (name = "io.mainframe.shopsystem.InvoicePDFError")]
-public errordomain InvoicePDFError {
- /* missing invoice data */
- NO_INVOICE_DATA,
- NO_INVOICE_DATE,
- NO_INVOICE_ID,
- NO_INVOICE_RECIPIENT,
-
- /* data not supported by renderer */
- ARTICLE_NAME_TOO_LONG,
- PRICE_TOO_HIGH,
- TOO_FAR_IN_THE_FUTURE
-}
-
-public struct InvoiceRecipient {
- public string firstname;
- public string lastname;
- public string street;
- public string postal_code;
- public string city;
- public string gender;
-}
-
-public struct InvoiceEntry {
- int timestamp;
- string article;
- Price price;
-}
-
[DBus (name = "io.mainframe.shopsystem.InvoicePDF")]
public class InvoicePDF {
/* A4 sizes (in points, 72 DPI) */
@@ -49,10 +20,10 @@ public class InvoicePDF {
private const double height = 841.88976; /* 297mm */
/* invoice content, which should appear in the PDF */
- public string invoice_id { set; get; }
+ public string invoice_id { set; owned get; }
public int64 invoice_date { set; get; }
- public InvoiceRecipient invoice_recipient { set; get; }
- public InvoiceEntry[] invoice_entries { set; get; }
+ public InvoiceRecipient invoice_recipient { set; owned get; }
+ public InvoiceEntry[] invoice_entries { set; owned get; }
/* pdf data */
private uint8[] data;
@@ -77,29 +48,27 @@ public class InvoicePDF {
};
public InvoicePDF() {
+ clear();
+ }
+
+ private void render_svg(Cairo.Context ctx, string file) {
+ var svg = new Rsvg.Handle.from_file(file);
+ svg.render_cairo(ctx);
}
private void draw_footer(Cairo.Context ctx) {
- /* TODO: get path from config file, support svg */
- var footer = new Cairo.ImageSurface.from_png("../../invoice/footer-line.png");
- ctx.set_source_surface(footer, 0, 817);
- ctx.paint();
+ ctx.save();
+ ctx.translate(-20, 818);
+ ctx.scale(1.42, 1.42);
+ render_svg(ctx, "../../invoice/footer-line.svg");
+ ctx.restore();
}
private void draw_logo(Cairo.Context ctx) {
- /* TODO: get path from config file, support svg */
- var logo = new Cairo.ImageSurface.from_png("../../invoice/logo.png");
-
- var pattern = new Cairo.Pattern.for_surface(logo);
- Cairo.Matrix scaler;
- pattern.get_matrix(out scaler);
- scaler.scale(1.41,1.41);
- scaler.translate(-364.5,-22.5);
- pattern.set_matrix(scaler);
- pattern.set_filter(Cairo.Filter.BEST);
-
- ctx.set_source(pattern);
- ctx.paint();
+ ctx.save();
+ ctx.translate(366,25);
+ render_svg(ctx, "../../invoice/logo.svg");
+ ctx.restore();
}
private void draw_address(Cairo.Context ctx) {
@@ -661,29 +630,14 @@ public class InvoicePDF {
public void clear() {
invoice_date = 0;
invoice_id = "";
- invoice_recipient = {};
+ invoice_recipient = {
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ };
invoice_entries = null;
}
}
-
-public static int main(string[] args) {
- Bus.own_name(
- BusType.SESSION,
- "io.mainframe.shopsystem.InvoicePDF",
- BusNameOwnerFlags.NONE,
- on_bus_aquired,
- () => {},
- () => stderr.printf ("Could not aquire name\n"));
-
- new MainLoop ().run ();
-
- return 0;
-}
-
-void on_bus_aquired(DBusConnection conn) {
- try {
- conn.register_object ("/io/mainframe/invoicepdf", new InvoicePDF());
- } catch (IOError e) {
- stderr.printf ("Could not register service\n");
- }
-}
diff --git a/src/pdf-invoice/test.vala b/src/pdf-invoice/test.vala
new file mode 100644
index 0000000..f9d21f6
--- /dev/null
+++ b/src/pdf-invoice/test.vala
@@ -0,0 +1,47 @@
+/* Copyright 2013, Sebastian Reichel <sre@ring0.de>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+public static int main(string args[]) {
+ PDFInvoice invoice = Bus.get_proxy_sync(BusType.SESSION, "io.mainframe.shopsystem.InvoicePDF", "/io/mainframe/shopsystem/invoicepdf");
+
+ InvoiceRecipient r = {
+ "Max",
+ "Mustermann",
+ "Foobar Straße 42",
+ "31337",
+ "Entenhausen",
+ "masculinum"
+ };
+
+ InvoiceEntry e1 = {
+ 1364271520,
+ "Club Mate",
+ 2342
+ };
+
+ /* set invoice data */
+ invoice.invoice_id = "TEST";
+ invoice.invoice_date = 1364271524;
+ invoice.invoice_recipient = r;
+ invoice.invoice_entries = {e1};
+
+ /* generate pdf */
+ var pdfdata = invoice.generate();
+
+ /* write pdf into file */
+ FileUtils.set_contents("test.pdf", (string) pdfdata, pdfdata.length);
+
+ return 0;
+}