1 module util;
2 
3 import std.stdio;
4 import std.string;
5 import std.datetime;
6 import std.conv;
7 import std.getopt;
8 
9 import libsecret.Collection;
10 import libsecret.Schema;
11 import libsecret.Service;
12 import libsecret.Secret;
13 import libsecret.c.functions;
14 import libsecret.c.types;
15 
16 import gio.Cancellable;
17 import glib.HashTable;
18 class User {
19         final HashTable createHashTable() {
20                 import gtkc.glib;
21 
22                 return new HashTable(g_str_hash, g_str_equal);
23         }
24 
25         /**
26         * Set the tech details section.
27         */
28         final void createSchema() {
29                 HashTable ht;
30                 ht = createHashTable();
31                 ht.insert(cast(void*) _userName, cast(void*) 0);
32                 ht.insert(cast(void*) _dateCreated, cast(void*) 0);
33                 ht.insert(cast(void*) _serviceName, cast(void*) 0);
34                 schema = new Schema(SCHEMA_NAME, SecretSchemaFlags.NONE, ht);
35         }
36 
37         void setPassword(string name, string testPassword) {
38                 HashTable attributes = createHashTable();
39                 attributes.insert(cast(void*) _userName, cast(void*) toStringz(name));
40                 attributes.insert(cast(void*) _serviceName, cast(void*) toStringz("tester"));
41 
42                 _label = text("Password for \"", name, "\" on \"tester\"");
43                 auto currentTime = text(Clock.currTime());
44 
45                 attributes.insert(cast(void*) _dateCreated, cast(void*) currentTime);
46                 Secret.passwordStorevSync(schema, attributes,
47                                 DEFAULT_COLLECTION, _label, testPassword, c);
48         }
49 
50         string getPassword(string name) {
51                 HashTable attributes = createHashTable();
52                 attributes.insert(cast(void*) _userName, cast(void*) toStringz(name));
53                 attributes.insert(cast(void*) _serviceName, cast(void*) toStringz("tester"));
54                 return Secret.passwordLookupvSync(schema, attributes, c);
55         }
56 
57         void deletePassword(string testName) {
58                 HashTable attributes = createHashTable();
59                 attributes.insert(cast(void*) _userName, cast(void*) testName);
60                 attributes.insert(cast(void*) _serviceName, cast(void*) toStringz("tester"));
61                 Secret.passwordClearvSync(schema, attributes, c);
62         }
63 
64         this() {
65                 c = new Cancellable();
66                 _userName = cast(char*) "Name";
67                 _dateCreated = cast(char*) "Date";
68                 _serviceName = cast(char*) "Service";
69                 createSchema();
70         }
71 
72 private:
73         enum SCHEMA_NAME = "org.test";
74         enum DEFAULT_COLLECTION = "default";
75 
76         Schema schema;
77         HashTable hashTable;
78         Cancellable c;
79 
80         char* _userName;
81         char* _dateCreated;
82         char* _serviceName;
83 
84         string _label;
85 }