]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/ConfigWriter.java
working backup and restore of configuration settings
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / ConfigWriter.java
1 /*
2  * Copyright © 2021 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.async;
19
20 import android.content.Context;
21 import android.net.Uri;
22 import android.util.JsonWriter;
23
24 import net.ktnx.mobileledger.db.Currency;
25 import net.ktnx.mobileledger.db.DB;
26 import net.ktnx.mobileledger.db.Profile;
27 import net.ktnx.mobileledger.db.TemplateAccount;
28 import net.ktnx.mobileledger.db.TemplateWithAccounts;
29 import net.ktnx.mobileledger.json.API;
30 import net.ktnx.mobileledger.model.Data;
31 import net.ktnx.mobileledger.utils.Misc;
32
33 import java.io.BufferedWriter;
34 import java.io.FileNotFoundException;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 import java.io.OutputStreamWriter;
38 import java.util.List;
39
40 public class ConfigWriter extends ConfigIO {
41     private final OnDoneListener onDoneListener;
42     private JsonWriter w;
43     public ConfigWriter(Context context, Uri uri, OnErrorListener onErrorListener,
44                         OnDoneListener onDoneListener) throws FileNotFoundException {
45         super(context, uri, onErrorListener);
46
47         this.onDoneListener = onDoneListener;
48     }
49     @Override
50     protected String getStreamMode() {
51         return "w";
52     }
53     @Override
54     protected void initStream() {
55         w = new JsonWriter(new BufferedWriter(
56                 new OutputStreamWriter(new FileOutputStream(pfd.getFileDescriptor()))));
57         w.setIndent("  ");
58     }
59     @Override
60     protected void processStream() throws IOException {
61         w.beginObject();
62         writeCommodities(w);
63         writeProfiles(w);
64         writeCurrentProfile(w);
65         writeConfigTemplates(w);
66         w.endObject();
67         w.flush();
68
69         if (onDoneListener != null)
70             Misc.onMainThread(onDoneListener::done);
71     }
72     private void writeKey(JsonWriter w, String key, String value) throws IOException {
73         if (value != null)
74             w.name(key)
75              .value(value);
76     }
77     private void writeKey(JsonWriter w, String key, Integer value) throws IOException {
78         if (value != null)
79             w.name(key)
80              .value(value);
81     }
82     private void writeKey(JsonWriter w, String key, Long value) throws IOException {
83         if (value != null)
84             w.name(key)
85              .value(value);
86     }
87     private void writeKey(JsonWriter w, String key, Float value) throws IOException {
88         if (value != null)
89             w.name(key)
90              .value(value);
91     }
92     private void writeKey(JsonWriter w, String key, Boolean value) throws IOException {
93         if (value != null)
94             w.name(key)
95              .value(value);
96     }
97     private void writeConfigTemplates(JsonWriter w) throws IOException {
98         List<TemplateWithAccounts> templates = DB.get()
99                                                  .getTemplateDAO()
100                                                  .getAllTemplatesWithAccountsSync();
101
102         if (templates.isEmpty())
103             return;
104
105         w.name("templates")
106          .beginArray();
107         for (TemplateWithAccounts t : templates) {
108             w.name(Keys.UUID)
109              .value(t.header.getUuid());
110             w.name(Keys.NAME)
111              .value(t.header.getName());
112             w.name(Keys.REGEX)
113              .value(t.header.getRegularExpression());
114             writeKey(w, Keys.TEST_TEXT, t.header.getTestText());
115             writeKey(w, Keys.DATE_YEAR, t.header.getDateYear());
116             writeKey(w, Keys.DATE_YEAR_GROUP, t.header.getDateYearMatchGroup());
117             writeKey(w, Keys.DATE_MONTH, t.header.getDateMonth());
118             writeKey(w, Keys.DATE_MONTH_GROUP, t.header.getDateMonthMatchGroup());
119             writeKey(w, Keys.DATE_DAY, t.header.getDateDay());
120             writeKey(w, Keys.DATE_DAY_GROUP, t.header.getDateDayMatchGroup());
121             writeKey(w, Keys.TRANSACTION, t.header.getTransactionDescription());
122             writeKey(w, Keys.TRANSACTION_GROUP, t.header.getTransactionDescriptionMatchGroup());
123             writeKey(w, Keys.COMMENT, t.header.getTransactionComment());
124             writeKey(w, Keys.COMMENT_GROUP, t.header.getTransactionCommentMatchGroup());
125             w.name(Keys.IS_FALLBACK)
126              .value(t.header.isFallback());
127             if (t.accounts.size() > 0) {
128                 w.name(Keys.ACCOUNTS)
129                  .beginArray();
130                 for (TemplateAccount a : t.accounts) {
131                     writeKey(w, Keys.NAME, a.getAccountName());
132                     writeKey(w, Keys.NAME_GROUP, a.getAccountNameMatchGroup());
133                     writeKey(w, Keys.COMMENT, a.getAccountComment());
134                     writeKey(w, Keys.COMMENT_GROUP, a.getAccountCommentMatchGroup());
135                     writeKey(w, Keys.AMOUNT, a.getAmount());
136                     writeKey(w, Keys.AMOUNT_GROUP, a.getAmountMatchGroup());
137                     writeKey(w, Keys.NEGATE_AMOUNT, a.getNegateAmount());
138                     writeKey(w, Keys.CURRENCY, a.getCurrency());
139                     writeKey(w, Keys.CURRENCY_GROUP, a.getCurrencyMatchGroup());
140                 }
141                 w.endArray();
142             }
143         }
144         w.endArray();
145     }
146     private void writeCommodities(JsonWriter w) throws IOException {
147         List<Currency> list = DB.get()
148                                 .getCurrencyDAO()
149                                 .getAllSync();
150         if (list.isEmpty())
151             return;
152         w.name(Keys.COMMODITIES)
153          .beginArray();
154         for (Currency c : list) {
155             w.beginObject();
156             writeKey(w, Keys.NAME, c.getName());
157             writeKey(w, Keys.POSITION, c.getPosition());
158             writeKey(w, Keys.HAS_GAP, c.getHasGap());
159             w.endObject();
160         }
161         w.endArray();
162     }
163     private void writeProfiles(JsonWriter w) throws IOException {
164         List<Profile> profiles = DB.get()
165                                    .getProfileDAO()
166                                    .getAllOrderedSync();
167
168         if (profiles.isEmpty())
169             return;
170
171         w.name(Keys.PROFILES)
172          .beginArray();
173         for (Profile p : profiles) {
174             w.beginObject();
175
176             w.name(Keys.NAME)
177              .value(p.getName());
178             w.name(Keys.UUID)
179              .value(p.getUuid());
180             w.name(Keys.URL)
181              .value(p.getUrl());
182             w.name(Keys.USE_AUTH)
183              .value(p.useAuthentication());
184             if (p.useAuthentication()) {
185                 w.name(Keys.AUTH_USER)
186                  .value(p.getAuthUser());
187                 w.name(Keys.AUTH_PASS)
188                  .value(p.getAuthPassword());
189             }
190             if (p.getApiVersion() != API.auto.toInt())
191                 w.name(Keys.API_VER)
192                  .value(p.getApiVersion());
193             w.name(Keys.CAN_POST)
194              .value(p.permitPosting());
195             if (p.permitPosting()) {
196                 String defaultCommodity = p.getDefaultCommodity();
197                 if (!defaultCommodity.isEmpty())
198                     w.name(Keys.DEFAULT_COMMODITY)
199                      .value(defaultCommodity);
200                 w.name(Keys.SHOW_COMMODITY)
201                  .value(p.getShowCommodityByDefault());
202                 w.name(Keys.SHOW_COMMENTS)
203                  .value(p.getShowCommentsByDefault());
204                 w.name(Keys.FUTURE_DATES)
205                  .value(p.getFutureDates());
206                 w.name(Keys.PREF_ACCOUNT)
207                  .value(p.getPreferredAccountsFilter());
208             }
209             w.name(Keys.COLOUR)
210              .value(p.getTheme());
211
212             w.endObject();
213         }
214         w.endArray();
215     }
216     private void writeCurrentProfile(JsonWriter w) throws IOException {
217         Profile currentProfile = Data.getProfile();
218         if (currentProfile == null)
219             return;
220
221         w.name(Keys.CURRENT_PROFILE)
222          .value(currentProfile.getUuid());
223     }
224
225     abstract static public class OnDoneListener {
226         public abstract void done();
227     }
228 }