Merge lp:~harp37/writer/export into lp:writer

Proposed by Anthony Huben
Status: Merged
Merged at revision: 79
Proposed branch: lp:~harp37/writer/export
Merge into: lp:writer
Diff against target: 155 lines (+151/-0)
1 file modified
src/Utils/FileExporter.vala (+151/-0)
To merge this branch: bzr merge lp:~harp37/writer/export
Reviewer Review Type Date Requested Status
Anthony Huben Pending
Review via email: mp+255868@code.launchpad.net
To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'src/Utils/FileExporter.vala'
2--- src/Utils/FileExporter.vala 1970-01-01 00:00:00 +0000
3+++ src/Utils/FileExporter.vala 2015-04-10 17:39:56 +0000
4@@ -0,0 +1,151 @@
5+/***
6+The MIT License (MIT)
7+
8+Copyright (c) 2014 Anthony Huben
9+
10+Permission is hereby granted, free of charge, to any person obtaining a copy
11+of this software and associated documentation files (the "Software"), to deal
12+in the Software without restriction, including without limitation the rights
13+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+copies of the Software, and to permit persons to whom the Software is
15+furnished to do so, subject to the following conditions:
16+
17+The above copyright notice and this permission notice shall be included in all
18+copies or substantial portions of the Software.
19+
20+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26+SOFTWARE.
27+
28+Inspired by Footnote.
29+***/
30+
31+namespace Writer.Utils {
32+
33+ public class ExportDialog : Gtk.FileChooserDialog {
34+
35+ GLib.HashTable<string, string> files;
36+ string current_ext = "";
37+
38+ const string DEFAULT = "text/rtf";
39+ const int PREFS_NUM = 1;
40+
41+ signal void filter_changed ();
42+
43+ public ExportDialog.export_document (List<Note> notes, string title, Gtk.Window window) {
44+ string[] bins = {"pdf"};
45+
46+ files = new GLib.HashTable<string, string> (str_hash, str_equal);
47+ files["text/rtf"] = "Rich Text Format (.rtf)";
48+ files["application/pdf"] = "Portable Document Format (.pdf)";
49+ build ();
50+
51+ var default_name = files[DEFAULT];
52+ var default_ext = default_name[default_name.index_of ("(")+1:-1];
53+ set_current_name (title + default_ext);
54+ current_ext = default_ext;
55+
56+ string str = "";
57+
58+ notify.connect ( (p)=> {
59+ if (p.get_name () == "filter")
60+ filter_changed ();
61+ });
62+
63+ response.connect ( (id)=> {
64+ if (id == Gtk.ResponseType.ACCEPT) {
65+ string ext = current_ext[1:current_ext.length];
66+
67+ var output = File.new_for_path (this.get_filename ());
68+ try {output.delete ();} catch (GLib.Error e) {};
69+ var data_stream = new DataOutputStream (output.create (FileCreateFlags.NONE));
70+ switch (ext) {
71+ case "rtf":
72+ // rework to get it working for documents
73+ foreach (Text a in text) {
74+ str += rtf (a);
75+ }
76+ break;
77+ }
78+ data_stream.put_string (str);
79+ } catch (GLib.Error e) {
80+ stderr.printf ("Error: %s\n", e.message);
81+ }
82+ }
83+ this.destroy ();
84+ }
85+ });
86+}
87+
88+private void build () {
89+ set_action (Gtk.FileChooserAction.SAVE);
90+ set_title ("");
91+ set_create_folders (true);
92+ set_do_overwrite_confirmation (true);
93+ set_default_response (Gtk.ResponseType.ACCEPT);
94+
95+ var entry = new Gtk.Entry ();
96+ ((Gtk.Box)get_child ()).get_children ().foreach ( (w)=> {
97+ ((Gtk.Container)w).foreach ( (d)=> {
98+ if (d as Gtk.Container != null) {
99+ ((Gtk.Container)d).foreach ( (f)=> {
100+ if (f as Gtk.Container != null) {
101+ ((Gtk.Container)f).foreach ( (g)=> {
102+ if (g as Gtk.Container != null) {
103+ ((Gtk.Container)g).foreach ( (h)=> {
104+ if (h as Gtk.Entry != null) {
105+ entry = (Gtk.Entry)h;
106+ }
107+ });
108+ }
109+ });
110+ }
111+ });
112+ }
113+ });
114+ });
115+
116+ filter_changed.connect ( ()=> {
117+ var name = entry.get_text ();
118+ if (name != null) {
119+ var ext_name = this.get_filter ().get_filter_name ();
120+ var ext = ext_name[ext_name.index_of ("(")+1:-1];
121+ if (name.has_suffix (current_ext))
122+ set_current_name (name[0:-4] + ext);
123+ else
124+ set_current_name (name + ext);
125+ current_ext = ext;
126+ }
127+ });
128+
129+
130+ var cancel = (Gtk.Button)add_button (Gtk.Stock.CANCEL, 1);
131+ cancel.clicked.connect ( ()=> {this.destroy ();});
132+ add_button (Gtk.Stock.SAVE, Gtk.ResponseType.ACCEPT);
133+
134+ files.foreach ( (mime, name) => {
135+ var filter = new Gtk.FileFilter ();
136+ filter.add_mime_type (mime);
137+ filter.set_filter_name (name);
138+ this.add_filter (filter);
139+ if (DEFAULT in mime)
140+ this.set_filter (filter);
141+ });
142+
143+ this.show_all ();
144+ }
145+
146+ private string rtf (Text a) {
147+ string r = "";
148+ string body = a.content.buffer.text.replace ("\\", "\\\\");
149+ body = body.replace("\a", "\\line\a");
150+ string document_body = @"\\cf1\a\\pard $body\\par\a\\line\\line";
151+ r += document_body;
152+ return r;
153+ }
154+ }
155+}

Subscribers

People subscribed via source and target branches

to all changes: