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.backup;
20 import android.util.JsonWriter;
22 import net.ktnx.mobileledger.backup.ConfigIO.Keys;
23 import net.ktnx.mobileledger.db.Currency;
24 import net.ktnx.mobileledger.db.DB;
25 import net.ktnx.mobileledger.db.Profile;
26 import net.ktnx.mobileledger.db.TemplateAccount;
27 import net.ktnx.mobileledger.db.TemplateWithAccounts;
28 import net.ktnx.mobileledger.json.API;
29 import net.ktnx.mobileledger.model.Data;
31 import java.io.BufferedWriter;
32 import java.io.IOException;
33 import java.io.OutputStream;
34 import java.io.OutputStreamWriter;
35 import java.util.List;
37 public class RawConfigWriter {
38 private final JsonWriter w;
39 public RawConfigWriter(OutputStream outputStream) {
40 w = new JsonWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
43 public void writeConfig() throws IOException {
47 writeCurrentProfile();
48 writeConfigTemplates();
52 private void writeKey(String key, String value) throws IOException {
57 private void writeKey(String key, Integer value) throws IOException {
62 private void writeKey(String key, Long value) throws IOException {
67 private void writeKey(String key, Float value) throws IOException {
72 private void writeKey(String key, Boolean value) throws IOException {
77 private void writeConfigTemplates() throws IOException {
78 List<TemplateWithAccounts> templates = DB.get()
80 .getAllTemplatesWithAccountsSync();
82 if (templates.isEmpty())
87 for (TemplateWithAccounts t : templates) {
91 .value(t.header.getUuid());
93 .value(t.header.getName());
95 .value(t.header.getRegularExpression());
96 writeKey(Keys.TEST_TEXT, t.header.getTestText());
97 writeKey(ConfigIO.Keys.DATE_YEAR, t.header.getDateYear());
98 writeKey(Keys.DATE_YEAR_GROUP, t.header.getDateYearMatchGroup());
99 writeKey(Keys.DATE_MONTH, t.header.getDateMonth());
100 writeKey(Keys.DATE_MONTH_GROUP, t.header.getDateMonthMatchGroup());
101 writeKey(Keys.DATE_DAY, t.header.getDateDay());
102 writeKey(Keys.DATE_DAY_GROUP, t.header.getDateDayMatchGroup());
103 writeKey(Keys.TRANSACTION, t.header.getTransactionDescription());
104 writeKey(Keys.TRANSACTION_GROUP, t.header.getTransactionDescriptionMatchGroup());
105 writeKey(Keys.COMMENT, t.header.getTransactionComment());
106 writeKey(Keys.COMMENT_GROUP, t.header.getTransactionCommentMatchGroup());
107 w.name(Keys.IS_FALLBACK)
108 .value(t.header.isFallback());
109 if (t.accounts.size() > 0) {
110 w.name(Keys.ACCOUNTS)
112 for (TemplateAccount a : t.accounts) {
115 writeKey(Keys.NAME, a.getAccountName());
116 writeKey(Keys.NAME_GROUP, a.getAccountNameMatchGroup());
117 writeKey(Keys.COMMENT, a.getAccountComment());
118 writeKey(Keys.COMMENT_GROUP, a.getAccountCommentMatchGroup());
119 writeKey(Keys.AMOUNT, a.getAmount());
120 writeKey(Keys.AMOUNT_GROUP, a.getAmountMatchGroup());
121 writeKey(Keys.NEGATE_AMOUNT, a.getNegateAmount());
122 writeKey(Keys.CURRENCY, a.getCurrency());
123 writeKey(Keys.CURRENCY_GROUP, a.getCurrencyMatchGroup());
134 private void writeCommodities() throws IOException {
135 List<Currency> list = DB.get()
140 w.name(Keys.COMMODITIES)
142 for (Currency c : list) {
144 writeKey(Keys.NAME, c.getName());
145 writeKey(Keys.POSITION, c.getPosition());
146 writeKey(Keys.HAS_GAP, c.getHasGap());
151 private void writeProfiles() throws IOException {
152 List<Profile> profiles = DB.get()
154 .getAllOrderedSync();
156 if (profiles.isEmpty())
159 w.name(Keys.PROFILES)
161 for (Profile p : profiles) {
170 w.name(Keys.USE_AUTH)
171 .value(p.useAuthentication());
172 if (p.useAuthentication()) {
173 w.name(Keys.AUTH_USER)
174 .value(p.getAuthUser());
175 w.name(Keys.AUTH_PASS)
176 .value(p.getAuthPassword());
178 if (p.getApiVersion() != API.auto.toInt())
180 .value(p.getApiVersion());
181 w.name(Keys.CAN_POST)
182 .value(p.permitPosting());
183 if (p.permitPosting()) {
184 String defaultCommodity = p.getDefaultCommodity();
185 if (!defaultCommodity.isEmpty())
186 w.name(Keys.DEFAULT_COMMODITY)
187 .value(defaultCommodity);
188 w.name(Keys.SHOW_COMMODITY)
189 .value(p.getShowCommodityByDefault());
190 w.name(Keys.SHOW_COMMENTS)
191 .value(p.getShowCommentsByDefault());
192 w.name(Keys.FUTURE_DATES)
193 .value(p.getFutureDates());
194 w.name(Keys.PREF_ACCOUNT)
195 .value(p.getPreferredAccountsFilter());
198 .value(p.getTheme());
204 private void writeCurrentProfile() throws IOException {
205 Profile currentProfile = Data.getProfile();
206 if (currentProfile == null)
209 w.name(Keys.CURRENT_PROFILE)
210 .value(currentProfile.getUuid());