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.
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.
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/>.
18 package net.ktnx.mobileledger.async;
20 import android.content.Context;
21 import android.net.Uri;
22 import android.util.JsonWriter;
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;
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;
40 public class ConfigWriter extends ConfigIO {
41 private final OnDoneListener onDoneListener;
43 public ConfigWriter(Context context, Uri uri, OnErrorListener onErrorListener,
44 OnDoneListener onDoneListener) throws FileNotFoundException {
45 super(context, uri, onErrorListener);
47 this.onDoneListener = onDoneListener;
50 protected String getStreamMode() {
54 protected void initStream() {
55 w = new JsonWriter(new BufferedWriter(
56 new OutputStreamWriter(new FileOutputStream(pfd.getFileDescriptor()))));
60 protected void processStream() throws IOException {
64 writeCurrentProfile(w);
65 writeConfigTemplates(w);
69 if (onDoneListener != null)
70 Misc.onMainThread(onDoneListener::done);
72 private void writeKey(JsonWriter w, String key, String value) throws IOException {
77 private void writeKey(JsonWriter w, String key, Integer value) throws IOException {
82 private void writeKey(JsonWriter w, String key, Long value) throws IOException {
87 private void writeKey(JsonWriter w, String key, Float value) throws IOException {
92 private void writeKey(JsonWriter w, String key, Boolean value) throws IOException {
97 private void writeConfigTemplates(JsonWriter w) throws IOException {
98 List<TemplateWithAccounts> templates = DB.get()
100 .getAllTemplatesWithAccountsSync();
102 if (templates.isEmpty())
107 for (TemplateWithAccounts t : templates) {
111 .value(t.header.getUuid());
113 .value(t.header.getName());
115 .value(t.header.getRegularExpression());
116 writeKey(w, Keys.TEST_TEXT, t.header.getTestText());
117 writeKey(w, Keys.DATE_YEAR, t.header.getDateYear());
118 writeKey(w, Keys.DATE_YEAR_GROUP, t.header.getDateYearMatchGroup());
119 writeKey(w, Keys.DATE_MONTH, t.header.getDateMonth());
120 writeKey(w, Keys.DATE_MONTH_GROUP, t.header.getDateMonthMatchGroup());
121 writeKey(w, Keys.DATE_DAY, t.header.getDateDay());
122 writeKey(w, Keys.DATE_DAY_GROUP, t.header.getDateDayMatchGroup());
123 writeKey(w, Keys.TRANSACTION, t.header.getTransactionDescription());
124 writeKey(w, Keys.TRANSACTION_GROUP, t.header.getTransactionDescriptionMatchGroup());
125 writeKey(w, Keys.COMMENT, t.header.getTransactionComment());
126 writeKey(w, Keys.COMMENT_GROUP, t.header.getTransactionCommentMatchGroup());
127 w.name(Keys.IS_FALLBACK)
128 .value(t.header.isFallback());
129 if (t.accounts.size() > 0) {
130 w.name(Keys.ACCOUNTS)
132 for (TemplateAccount a : t.accounts) {
135 writeKey(w, Keys.NAME, a.getAccountName());
136 writeKey(w, Keys.NAME_GROUP, a.getAccountNameMatchGroup());
137 writeKey(w, Keys.COMMENT, a.getAccountComment());
138 writeKey(w, Keys.COMMENT_GROUP, a.getAccountCommentMatchGroup());
139 writeKey(w, Keys.AMOUNT, a.getAmount());
140 writeKey(w, Keys.AMOUNT_GROUP, a.getAmountMatchGroup());
141 writeKey(w, Keys.NEGATE_AMOUNT, a.getNegateAmount());
142 writeKey(w, Keys.CURRENCY, a.getCurrency());
143 writeKey(w, Keys.CURRENCY_GROUP, a.getCurrencyMatchGroup());
154 private void writeCommodities(JsonWriter w) throws IOException {
155 List<Currency> list = DB.get()
160 w.name(Keys.COMMODITIES)
162 for (Currency c : list) {
164 writeKey(w, Keys.NAME, c.getName());
165 writeKey(w, Keys.POSITION, c.getPosition());
166 writeKey(w, Keys.HAS_GAP, c.getHasGap());
171 private void writeProfiles(JsonWriter w) throws IOException {
172 List<Profile> profiles = DB.get()
174 .getAllOrderedSync();
176 if (profiles.isEmpty())
179 w.name(Keys.PROFILES)
181 for (Profile p : profiles) {
190 w.name(Keys.USE_AUTH)
191 .value(p.useAuthentication());
192 if (p.useAuthentication()) {
193 w.name(Keys.AUTH_USER)
194 .value(p.getAuthUser());
195 w.name(Keys.AUTH_PASS)
196 .value(p.getAuthPassword());
198 if (p.getApiVersion() != API.auto.toInt())
200 .value(p.getApiVersion());
201 w.name(Keys.CAN_POST)
202 .value(p.permitPosting());
203 if (p.permitPosting()) {
204 String defaultCommodity = p.getDefaultCommodity();
205 if (!defaultCommodity.isEmpty())
206 w.name(Keys.DEFAULT_COMMODITY)
207 .value(defaultCommodity);
208 w.name(Keys.SHOW_COMMODITY)
209 .value(p.getShowCommodityByDefault());
210 w.name(Keys.SHOW_COMMENTS)
211 .value(p.getShowCommentsByDefault());
212 w.name(Keys.FUTURE_DATES)
213 .value(p.getFutureDates());
214 w.name(Keys.PREF_ACCOUNT)
215 .value(p.getPreferredAccountsFilter());
218 .value(p.getTheme());
224 private void writeCurrentProfile(JsonWriter w) throws IOException {
225 Profile currentProfile = Data.getProfile();
226 if (currentProfile == null)
229 w.name(Keys.CURRENT_PROFILE)
230 .value(currentProfile.getUuid());
233 abstract static public class OnDoneListener {
234 public abstract void done();