4
Copyright (c) 2014 Anthony Huben
6
Permission is hereby granted, free of charge, to any person obtaining a copy
7
of this software and associated documentation files (the "Software"), to deal
8
in the Software without restriction, including without limitation the rights
9
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
copies of the Software, and to permit persons to whom the Software is
11
furnished to do so, subject to the following conditions:
13
The above copyright notice and this permission notice shall be included in all
14
copies or substantial portions of the Software.
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
namespace Writer.Utils {
29
public class ExportDialog : Gtk.FileChooserDialog {
31
GLib.HashTable<string, string> files;
32
string current_ext = "";
34
const string DEFAULT = "text/rtf";
35
const int PREFS_NUM = 1;
37
signal void filter_changed ();
39
public ExportDialog.export_document (List<Note> notes, string title, Gtk.Window window) {
40
string[] bins = {"pdf"};
42
files = new GLib.HashTable<string, string> (str_hash, str_equal);
43
files["text/rtf"] = "Rich Text Format (.rtf)";
44
files["application/pdf"] = "Portable Document Format (.pdf)";
47
var default_name = files[DEFAULT];
48
var default_ext = default_name[default_name.index_of ("(")+1:-1];
49
set_current_name (title + default_ext);
50
current_ext = default_ext;
54
notify.connect ( (p)=> {
55
if (p.get_name () == "filter")
59
response.connect ( (id)=> {
60
if (id == Gtk.ResponseType.ACCEPT) {
61
string ext = current_ext[1:current_ext.length];
63
var output = File.new_for_path (this.get_filename ());
64
try {output.delete ();} catch (GLib.Error e) {};
65
var data_stream = new DataOutputStream (output.create (FileCreateFlags.NONE));
68
// rework to get it working for documents
69
foreach (Text a in text) {
74
data_stream.put_string (str);
75
} catch (GLib.Error e) {
76
stderr.printf ("Error: %s\n", e.message);
84
private void build () {
85
set_action (Gtk.FileChooserAction.SAVE);
87
set_create_folders (true);
88
set_do_overwrite_confirmation (true);
89
set_default_response (Gtk.ResponseType.ACCEPT);
91
var entry = new Gtk.Entry ();
92
((Gtk.Box)get_child ()).get_children ().foreach ( (w)=> {
93
((Gtk.Container)w).foreach ( (d)=> {
94
if (d as Gtk.Container != null) {
95
((Gtk.Container)d).foreach ( (f)=> {
96
if (f as Gtk.Container != null) {
97
((Gtk.Container)f).foreach ( (g)=> {
98
if (g as Gtk.Container != null) {
99
((Gtk.Container)g).foreach ( (h)=> {
100
if (h as Gtk.Entry != null) {
101
entry = (Gtk.Entry)h;
112
filter_changed.connect ( ()=> {
113
var name = entry.get_text ();
115
var ext_name = this.get_filter ().get_filter_name ();
116
var ext = ext_name[ext_name.index_of ("(")+1:-1];
117
if (name.has_suffix (current_ext))
118
set_current_name (name[0:-4] + ext);
120
set_current_name (name + ext);
126
var cancel = (Gtk.Button)add_button (Gtk.Stock.CANCEL, 1);
127
cancel.clicked.connect ( ()=> {this.destroy ();});
128
add_button (Gtk.Stock.SAVE, Gtk.ResponseType.ACCEPT);
130
files.foreach ( (mime, name) => {
131
var filter = new Gtk.FileFilter ();
132
filter.add_mime_type (mime);
133
filter.set_filter_name (name);
134
this.add_filter (filter);
136
this.set_filter (filter);
142
private string rtf (Text a) {
144
string body = a.content.buffer.text.replace ("\\", "\\\\");
145
body = body.replace("\a", "\\line\a");
146
string document_body = @"\\cf1\a\\pard $body\\par\a\\line\\line";