Switch to unified view

a/cfgui/confmain.cpp b/cfgui/confmain.cpp
...
...
40
#ifndef DATADIR
40
#ifndef DATADIR
41
#define DATADIR "/usr/share/upmpdcli"
41
#define DATADIR "/usr/share/upmpdcli"
42
#endif
42
#endif
43
string g_datadir(DATADIR "/");
43
string g_datadir(DATADIR "/");
44
44
45
// g_csdef holds the defaults from the XML. g_csout values from a file
46
// specified on the command line or opened through the menu. The
47
// latter values override the defaults and will be output in addition
48
// to user changes.
45
static ConfSimple g_csdef, g_csout;
49
static ConfSimple g_csdef, g_csout;
50
// The file we write to. Not necessarily the same as the input one
51
// (same if coming from the command line).
46
static string g_outfile;
52
static string g_outfile;
47
53
54
// Pour the values from src into dest
48
void confPourInto(ConfSimple& dest, const ConfSimple& src)
55
void confMergeInto(ConfSimple& dest, const ConfSimple& src)
49
{
56
{
50
    for (const auto& key : src.getSubKeys()) {
57
    for (const auto& key : src.getSubKeys()) {
51
        for (const auto& nm : src.getNames(key)) {
58
        for (const auto& nm : src.getNames(key)) {
52
            string val;
59
            string val;
53
            src.get(nm, val, key);
60
            src.get(nm, val, key);
54
            dest.set(nm, val, key);
61
            dest.set(nm, val, key);
55
            //cerr << "confPourInto: set " << key << ":" << nm << endl;
62
            //cerr << "confMergeInto: set " << key << ":" << nm << endl;
56
        }
57
    }
63
        }
64
    }
58
}
65
}
59
66
60
#if 0
61
static string qs2utf8s(const QString& qs)
67
static string qs2utf8s(const QString& qs)
62
{
68
{
63
    return string((const char *)qs.toUtf8());
69
    return string((const char *)qs.toUtf8());
64
}
70
}
65
#endif
66
67
static string qs2locals(const QString& qs)
71
static string qs2locals(const QString& qs)
68
{
72
{
69
    return string((const char *)qs.toLocal8Bit());
73
    return string((const char *)qs.toLocal8Bit());
70
}
74
}
71
75
...
...
127
131
128
132
129
class MyConfLinkFactCS : public confgui::ConfLinkFact {
133
class MyConfLinkFactCS : public confgui::ConfLinkFact {
130
public:
134
public:
131
    MyConfLinkFactCS(ConfSimple *cs, ConfSimple *csdef) 
135
    MyConfLinkFactCS(ConfSimple *cs, ConfSimple *csdef) 
132
        : m_cs(cs), m_csdef(csdef) {
136
        : m_cs(cs), m_csdef(csdef) { }
133
    }
134
    virtual ConfLink operator()(const QString& nm) {
137
    virtual ConfLink operator()(const QString& nm) {
135
        ConfLinkRep *lnk = new ConfLinkCS(m_cs, m_csdef,
138
        return ConfLink(new ConfLinkCS(m_cs, m_csdef, qs2utf8s(nm)));
136
                                          (const char *)nm.toUtf8());
137
        return ConfLink(lnk);
138
    }
139
    }
139
    ConfSimple *m_cs;
140
    ConfSimple *m_cs;
140
    ConfSimple *m_csdef;
141
    ConfSimple *m_csdef;
141
};
142
};
142
143
...
...
175
    if (dialog.exec() != QDialog::Accepted)
176
    if (dialog.exec() != QDialog::Accepted)
176
        return false;
177
        return false;
177
    string f = qs2locals(dialog.selectedFiles().first());
178
    string f = qs2locals(dialog.selectedFiles().first());
178
    ConfSimple cs(f.c_str(), 1);
179
    ConfSimple cs(f.c_str(), 1);
179
    if (cs.ok()) {
180
    if (cs.ok()) {
181
        g_csout.clear();
180
        confPourInto(g_csout, cs);
182
        confMergeInto(g_csout, cs);
181
        m_tabs->reloadPanels();
183
        m_tabs->reloadPanels();
182
        return true;
184
        return true;
183
    } else {
185
    } else {
184
        QMessageBox::warning(0, "upmpdcli-config", tr("File parse failed"));
186
        QMessageBox::warning(0, "upmpdcli-config", tr("File parse failed"));
185
    }
187
    }
...
...
193
    dialog.setWindowModality(Qt::WindowModal);
195
    dialog.setWindowModality(Qt::WindowModal);
194
    dialog.setAcceptMode(QFileDialog::AcceptSave);
196
    dialog.setAcceptMode(QFileDialog::AcceptSave);
195
    if (dialog.exec() != QDialog::Accepted)
197
    if (dialog.exec() != QDialog::Accepted)
196
        return false;
198
        return false;
197
    g_outfile = qs2locals(dialog.selectedFiles().first());
199
    g_outfile = qs2locals(dialog.selectedFiles().first());
198
    m_tabs->acceptChanges();
199
    return saveToFile();
200
    return saveToFile();
200
}
201
}
201
202
202
bool MainWindow::save()
203
bool MainWindow::save()
203
{
204
{
204
    cerr << "MainWindow::save: outfile: " << g_outfile << endl;
205
    //cerr << "MainWindow::save: outfile: " << g_outfile << endl;
205
    
206
    if (g_outfile.empty()) {
206
    if (g_outfile.empty()) {
207
        return saveAs();
207
        return saveAs();
208
    } else {
208
    } else {
209
        m_tabs->acceptChanges();
210
        return saveToFile();
209
        return saveToFile();
211
    }
210
    }
212
}
211
}
213
212
214
bool MainWindow::saveToFile()
213
bool MainWindow::saveToFile()
215
{
214
{
215
    m_tabs->acceptChanges();
216
    ConfSimple fconf(g_outfile.c_str());
216
    ConfSimple fconf(g_outfile.c_str());
217
    fconf.holdWrites(true);
217
    fconf.holdWrites(true);
218
    confPourInto(fconf, g_csout);
218
    confMergeInto(fconf, g_csout);
219
    return fconf.holdWrites(false);
219
    bool ret = fconf.holdWrites(false);
220
    if (!ret) { 
221
        QMessageBox::warning(0, "upmpdcli-config",
222
                             tr("Save to file failed: ") + u8s2qs(g_outfile));
223
    }
224
    return ret;
220
}
225
}
221
226
222
void MainWindow::closeEvent(QCloseEvent *event)
227
void MainWindow::closeEvent(QCloseEvent *event)
223
{
228
{
224
    // Should test for unsaved mods here
229
    // Should test for unsaved mods here
...
...
296
    // copy-assign the real confsimple to the provisional one once the
301
    // copy-assign the real confsimple to the provisional one once the
297
    // GUI is built, and reload the defaults. Slightly inefficient,
302
    // GUI is built, and reload the defaults. Slightly inefficient,
298
    // but does the job.
303
    // but does the job.
299
    string data;
304
    string data;
300
    g_csout = ConfSimple(data);
305
    g_csout = ConfSimple(data);
306
    // If outfile was specd on the command line, it's both the input
307
    // and the output. Get existing parameters.
308
    if (!g_outfile.empty()) {
309
        ConfSimple cs(g_outfile.c_str(), 1);
310
        if (cs.ok()) {
311
            confMergeInto(g_csout, cs);
312
        } else {
313
            QMessageBox::warning(0, "upmpdcli-config", "File parse failed");
314
            return 1;
315
        }
316
    }
301
    MyConfLinkFactCS fact(&g_csout, &g_csdef);
317
    MyConfLinkFactCS fact(&g_csout, &g_csdef);
302
318
303
    string toptext;
319
    string toptext;
304
    ConfTabsW *w = xmlToConfGUI(stream.str(), toptext, &fact, 0);
320
    ConfTabsW *w = xmlToConfGUI(stream.str(), toptext, &fact, 0);
305
    if (!w) {
321
    if (!w) {