[gnome-contacts/wip/nielsdg/vcard-import: 47/47] WIP




commit bb9f4d513f56be30e166817bb9a1c37c2b352c68
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Fri May 20 17:15:53 2022 +0200

    WIP

 src/contacts-app.vala                              |  1 +
 src/contacts-import-operation.vala                 |  2 +-
 src/contacts-main-window.vala                      | 10 ++++++--
 src/io/contacts-io-exporter.vala                   | 28 +++++++++++++++++++++-
 src/io/contacts-io-vcard-exporter.vala             | 12 ++++------
 tests/io/vcard/meson.build                         |  3 ++-
 ...minimal.vala => test-vcard-minimal-import.vala} |  0
 7 files changed, 44 insertions(+), 12 deletions(-)
---
diff --git a/src/contacts-app.vala b/src/contacts-app.vala
index 26ffc7f7..e6e99a76 100644
--- a/src/contacts-app.vala
+++ b/src/contacts-app.vala
@@ -307,6 +307,7 @@ public class Contacts.App : Adw.Application {
       }
       base.quit ();
     });
+  }
 
   private void on_import (SimpleAction action, Variant? param) {
     var chooser = new Gtk.FileChooserNative ("Select contact file",
diff --git a/src/contacts-import-operation.vala b/src/contacts-import-operation.vala
index 784c8059..2f7cd85d 100644
--- a/src/contacts-import-operation.vala
+++ b/src/contacts-import-operation.vala
@@ -20,7 +20,7 @@ using Folks;
 /**
  * Launches a subprocess to deal with the import of a Contact file
  */
-public class Contacts.ImportOperation : Object, Operation {
+public class Contacts.ImportOperation : Object { // XXX Operation
 
   private unowned Store store;
 
diff --git a/src/contacts-main-window.vala b/src/contacts-main-window.vala
index bd38db76..7bac40fa 100644
--- a/src/contacts-main-window.vala
+++ b/src/contacts-main-window.vala
@@ -560,8 +560,14 @@ public class Contacts.MainWindow : Adw.ApplicationWindow {
 
     var individuals = bitset_to_individuals (this.store.filter_model,
                                              selection);
-    var ret = exporter.export_to_string (individuals.to_array ());
-    warning ("===============VCARD===============\n%s\n==============", ret);
+    exporter.export_to_string.begin (individuals.to_array (), (obj, res) => {
+      try {
+        var ret = exporter.export_to_string.end (res);
+        warning ("===============VCARD===============\n%s\n==============", ret);
+      } catch (Error e) {
+        warning ("ERROR: %s", e.message);
+      }
+    });
   }
 
   // Little helper
diff --git a/src/io/contacts-io-exporter.vala b/src/io/contacts-io-exporter.vala
index 18cfc7ab..6021e806 100644
--- a/src/io/contacts-io-exporter.vala
+++ b/src/io/contacts-io-exporter.vala
@@ -27,5 +27,31 @@ using Folks;
  */
 public abstract class Contacts.Io.Exporter {
 
-  public abstract string export_to_string (Individual[] individuals) throws GLib.Error;
+  /**
+   * The base method that exports the given individuals to some output defined
+   * by the stream parameter.
+   */
+  public abstract async void export (Individual[] individuals,
+                                     GLib.OutputStream stream) throws GLib.Error;
+
+  /**
+   * A wrapper method to export to a given file
+   */
+  public async void export_to_file (Individual[] individuals,
+                                    GLib.File file) throws GLib.Error {
+    var stream = file.create (FileCreateFlags.NONE);
+    yield export (individuals, stream);
+  }
+
+  /**
+   * A wrapper around the export method, to return a string
+   */
+  public async string export_to_string (Individual[] individuals) throws GLib.Error {
+    var stream = new MemoryOutputStream.resizable ();
+
+    yield export (individuals, stream);
+    size_t written;
+    stream.write_all ({ '\0' }, out written); // Write the null terminator
+    return (string) stream.steal_data ();
+  }
 }
diff --git a/src/io/contacts-io-vcard-exporter.vala b/src/io/contacts-io-vcard-exporter.vala
index 278c9fb8..82806090 100644
--- a/src/io/contacts-io-vcard-exporter.vala
+++ b/src/io/contacts-io-vcard-exporter.vala
@@ -37,21 +37,19 @@ public class Contacts.Io.VCardExporter : Contacts.Io.Exporter {
   public VCardExporter () {
   }
 
-  public override string export_to_string (Individual[] individuals) throws GLib.Error {
-    StringBuilder result = new StringBuilder ();
-
+  public override async void export (Individual[] individuals,
+                                     GLib.OutputStream stream) throws GLib.Error {
     foreach (unowned var individual in individuals) {
       // XXX at a certain point, we should aggregate personas, but that
       // requires more thinking through
 
       foreach (var persona in individual.personas) {
         string vcard_str = persona_to_vcard (persona);
-        result.append (vcard_str);
-        result.append_c ('\n');
+        size_t written;
+        stream.write_all (vcard_str.data, out written);
+        stream.write_all ("\n".data, out written);
       }
     }
-
-    return result.str;
   }
 
   private string persona_to_vcard (Persona persona) {
diff --git a/tests/io/vcard/meson.build b/tests/io/vcard/meson.build
index e3946e3f..eef536ac 100644
--- a/tests/io/vcard/meson.build
+++ b/tests/io/vcard/meson.build
@@ -1,5 +1,6 @@
 io_vcard_files = [
-  'minimal',
+  # 'minimal',
+  # XXX
 ]
 
 foreach vcard_name : io_vcard_files
diff --git a/tests/io/vcard/test-vcard-minimal.vala b/tests/io/vcard/test-vcard-minimal-import.vala
similarity index 100%
rename from tests/io/vcard/test-vcard-minimal.vala
rename to tests/io/vcard/test-vcard-minimal-import.vala


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]