summaryrefslogtreecommitdiffstats
path: root/abs/core/mythinstall/MythVantage-app/mythinstall/settemplate.cpp
blob: def6ae6d770495808415276953eb48e2d987c76b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include <mythcontext.h>
#include "installsettings.h"
#include "settemplate.h"
#include <stdlib.h>


void create_temp_table (QString create_table_name,QString like_name)
{
    MSqlQuery query(MSqlQuery::InitCon());
    query.prepare("create table " +  create_table_name + " like  " +   like_name + ";" );
    query.exec();
    if ( like_name == "settings")
    {
        query.prepare("Alter table  " + create_table_name +  " add unique ( value ) ; " );
        query.exec();
    }
    query.prepare(" truncate " + create_table_name + ";" );
    query.exec();
};

void drop_temp_table (QString tname)
{
    MSqlQuery query(MSqlQuery::InitCon());
    query.prepare("drop table " +  tname + ";" );
    query.exec();
}

void restart_frontend ()
{

    QString cmdtxt;
    cmdtxt=MV_ROOT  ;
    cmdtxt.append("bin/systemconfig.sh  reloadfe" );
    //cout << "Calling systemconfig.sh reloadfe" << endl;
    int sysRet = system(qPrintable(cmdtxt));
    if (sysRet < 0)
        LOG(VB_GENERAL, LOG_ERR, "Error running systemconfig.sh reloadfe");
}

void choosetemplate (QString templateop, QString templatename  , QString templatehostname = "")
{
    if ( templateop == ""  )
    {
        MythCtemplate setting;
//         setting.load();
//         setting.save();
//        setting.exec();
    };

    QString thistemplate;
    QString thisoperation;
    QString thishost;
    //QString runcommand = "/root/restore_default_settings.sh";
    QString runcommand = "echo ";
    // //cout <<  templateop << endl;
    // //cout << templatename << endl;
    // //cout << templatehostname << endl;

    if (templateop == "")
        thisoperation = gCoreContext->GetSetting("HOSTtemplatetype");
    else
        thisoperation = templateop;

    thistemplate = "none";


    if ( thisoperation == "save" )
    {
        if (templatename == "" )
            thistemplate = gCoreContext->GetSetting("HostTemplateSave");
        else
            thistemplate = templatename;

        savesettings(thistemplate);
    }
    else if ( thisoperation == "restore" )
    {
        if (templatename == "" )
            thistemplate = gCoreContext->GetSetting("HostTemplateRestore");
        else
            thistemplate = templatename;

        if ( thistemplate == "default_1")
            c_from("default_1" , thistemplate);
        else if ( thistemplate == "default_2")
            c_from("default_2" ,thistemplate);
        else
            restoresettings(thistemplate);

    }
    else if ( thisoperation == "copy_from" )
    {
        if (templatename == "" )
        {
            thishost = gCoreContext->GetSetting("HostSelectcopy_from");
            thistemplate = gCoreContext->GetSetting("HostTemplateRestore");
        }
        else
        {
            thishost=templatehostname;
            thistemplate=templatename;
        };

        c_from(thishost,thistemplate);
    }


//	if ( thistemplate != "none" )
//	{
// 	    std:://cout <<  runcommand << endl;
    //   system(runcommand);
    //}
};


void savesettings (QString templatenumber )
{
    //cout << "SAVING....." <<   templatenumber  << endl;
    QString templatename = "settings_" + templatenumber ;
    // Remove old saved values
    MSqlQuery query(MSqlQuery::InitCon());
    query.prepare( "delete from "  + templatename +  " where hostname=:HOSTNAME   ;"  );
    query.bindValue( ":HOSTNAME" ,   gCoreContext->GetHostName()  );
    query.exec();
    //insert all new settings
    query.prepare( "replace into "  + templatename +  " (select * from settings where hostname=:HOSTNAME  ) ;"  ) ;
    query.bindValue( ":HOSTNAME" ,   gCoreContext->GetHostName()  );
    query.exec();

    // repeat for keybindings
    templatename = "keybindings_" + templatenumber ;
    query.prepare( "delete from "  + templatename +  " where hostname=:HOSTNAME   ;"  );
    query.bindValue( ":HOSTNAME" ,   gCoreContext->GetHostName()  );
    query.exec();

    query.prepare( "replace into "  + templatename +  " (select * from keybindings  where hostname=:HOSTNAME  ) ;"  );
    query.bindValue( ":HOSTNAME" ,   gCoreContext->GetHostName()  );
    query.exec();

}

void restoresettings (QString templatenumber )
{
//cout << "RESTORING....." << templatenumber << endl;
// DEFAULT settings are handled by the copy from routine
    MSqlQuery query(MSqlQuery::InitCon());
    QStringList tablelist ;
    QStringList::Iterator it;
    QString current_table;
    tablelist.append ("settings");
    tablelist.append ("keybindings");
    QString templatename;

    for ( it = tablelist.begin(); it != tablelist.end(); ++it )
    {
        current_table = *it ;
        //find template table to use
        QString templatename =  current_table + "_" + templatenumber ;
        QString temptable="temp_table_" + current_table + "_" +   gCoreContext->GetHostName() ;
        // create temp table for merging settings,  The merge is needed to ac//coutn for any new settings.
        create_temp_table(temptable, current_table );

        // copy in current settings
        query.prepare( "replace into  " + temptable + "  (select * from  " + current_table + "  where hostname=:HOSTNAME ) ; "  );
        query.bindValue( ":HOSTNAME" ,   gCoreContext->GetHostName()  );
        query.exec();

        // need to remove all non HOST settigns

        // copy in stored settings
        query.prepare( "replace into  " + temptable + "  (select * from  " + templatename + "  where hostname=:HOSTNAME ) ; "  );
        query.bindValue( ":HOSTNAME" ,   gCoreContext->GetHostName()  );
        query.exec();

        // remove current settings.  Need to remove because the old table allows for duplicates and replace into doesn' seem to "replace"
        query.prepare( "delete from " + current_table + "  where hostname=:HOSTNAME ;"  );
        query.bindValue( ":HOSTNAME" ,   gCoreContext->GetHostName()  );
        query.exec();


        // copy new settings from temp to current
        query.prepare( "replace into " + current_table  + "   (select * from  " + temptable + "  );"  );
        query.exec();

        // drop temptable
        drop_temp_table(temptable);

    }
    restart_frontend();
};

void c_from(QString copyhost, QString templatenumber)
{
    //cout << "Copying....." + copyhost + " " + "templatenumber"   << endl;
    MSqlQuery query(MSqlQuery::InitCon());
    //Create temp table, copy in settings from host_template, update hostname for new host,copy temp_table to settings.
    QStringList tablelist ;
    QStringList::Iterator it;
    QString current_table ;
    tablelist.append ("settings");
    tablelist.append ("keybindings");
    QString templatename;
    QString temptable;
    for ( it = tablelist.begin(); it != tablelist.end(); ++it )
    {
        current_table = *it ;
        //cout << current_table << endl;
        //find template table to use
        if  ( templatenumber == "Current" )
            templatename =  current_table  ;
        else
            templatename =  current_table + "_" + templatenumber ;

        temptable="temp_table_" + current_table + "_" +   gCoreContext->GetHostName() ;
        // create temp table for merging settings

        create_temp_table(temptable, current_table );
        //cout << temptable + "  "         + current_table << endl;

        //  copy current settings from  this host  into temptable minus all Mythvantage settings
        if ( current_table == "settings")
        {
            query.prepare( "replace into  " + temptable + "    (select * from  " + current_table + "  where hostname=:HOSTNAME and value not like 'HOST%'  ) ; "  );
            query.bindValue( ":HOSTNAME" ,   gCoreContext->GetHostName()  );
        }
        else
            query.prepare( "replace into  " + temptable + "    (select * from  " + current_table + " where hostname=:HOSTNAME ) ; "  );
        query.bindValue( ":HOSTNAME" ,   gCoreContext->GetHostName()  );
        query.exec();

        // update hostname to match whatever the hostname of the template is
        query.prepare ("update " + temptable + "  set hostname=:HOSTNAME ; " );
        query.bindValue( ":HOSTNAME" ,  copyhost  );
        query.exec();

        // copy current settings from copy host  into temptable minus all Mythvantage settings
        if ( current_table == "settings")
            query.prepare( "replace into  " + temptable + "    (select * from  " + templatename  + "  where hostname=:HOSTNAME and value not like 'HOST%'  ) ; "  );

        else
            query.prepare( "replace into  " + temptable + "    (select * from  " + templatename  + "  where hostname=:HOSTNAME  ) ; "  );

        query.bindValue( ":HOSTNAME" ,  copyhost  );
        query.exec();

        //update hostname
        query.prepare ("update " + temptable + "  set hostname=:HOSTNAME ; " );
        query.bindValue( ":HOSTNAME" ,   gCoreContext->GetHostName()  );
        query.exec();

        // delete old settings from settings table
        if ( current_table == "settings")
        {
            query.prepare( "delete from " + current_table + "   where hostname=:HOSTNAME and value not like 'HOST%' ;"  );
            query.bindValue( ":HOSTNAME" ,   gCoreContext->GetHostName()  );
        }
        else
        {
            query.prepare( "delete from " + current_table + "   where hostname=:HOSTNAME ;"  );
            query.bindValue( ":HOSTNAME" ,   gCoreContext->GetHostName()  );
        }
        query.exec();

        // copy settings from temptable to settings
        if ( current_table == "settings")
            query.prepare( "replace into " + current_table + "   (select * from  " + temptable + " where value not like 'HOST% ' ) ;"  );
        else
            query.prepare( "replace into " + current_table + "   (select * from  " + temptable + " );"  );


        query.exec();

        //  drop temptable
        // drop_temp_table(temptable);


        restart_frontend();
    };


};




static HostComboBoxSetting *HOSTtemplatetype()
{
    HostComboBoxSetting *gc = new HostComboBoxSetting("HOSTtemplatetype");
    gc->setLabel(QObject::tr("Template options"));
    gc->addSelection("Do Nothing") ;
    gc->addSelection("restore");
    gc->addSelection("save") ;
    gc->addSelection("copy_from");

    gc->setHelpText(QObject::tr("Select the name to save, this will make a copy of your keybindings and videoplayback.  Choose none if you do not wish to change anything."));

    return gc;
}

static HostComboBoxSetting *HostTemplateSave()
{
    HostComboBoxSetting *gc = new HostComboBoxSetting("HostTemplateSave");
    gc->setLabel(QObject::tr("Save Template"));
//   gc->addSelection("none");
    gc->addSelection("user1") ;
    gc->addSelection("user2") ;
    gc->addSelection("user3") ;
    gc->setHelpText(QObject::tr("Select the name to save, this will make a copy of your keybindings and videoplayback.  Choose none if you do not wish to change anything."));

    return gc;
}
static HostComboBoxSetting *HostTemplateCopy_restore()
{
    HostComboBoxSetting *gc = new HostComboBoxSetting("HostTemplateRestore");
    gc->setLabel(QObject::tr("Template"));
    //  gc->addSelection("none");
    gc->addSelection("Current");
    gc->addSelection("user1") ;
    gc->addSelection("user2") ;
    gc->addSelection("user3") ;
    gc->setHelpText(QObject::tr("Select the template you wish to restore.  This will change the way Myth repsonds to you remote buttons.  Choose none if you do not wish to change anything."));

    return gc;
}

static HostComboBoxSetting *HostTemplateRestore()
{
    HostComboBoxSetting *gc = new HostComboBoxSetting("HostTemplateRestore");
    gc->setLabel(QObject::tr("Template"));
    //  gc->addSelection("none");
    gc->addSelection("default_1");
    gc->addSelection("default_2");
    gc->addSelection("user1") ;
    gc->addSelection("user2") ;
    gc->addSelection("user3") ;
    gc->setHelpText(QObject::tr("Select the template you wish to restore.  This will change the way Myth repsonds to you remote buttons.  Choose none if you do not wish to change anything."));

    return gc;
}

static HostComboBoxSetting *HostSelectcopy_from()
{
    HostComboBoxSetting *gc = new HostComboBoxSetting("HostSelectcopy_from");
    gc->setLabel(QObject::tr("Host"));
    //  gc->addSelection("none");

    QString tempItem;

    MSqlQuery query(MSqlQuery::InitCon());

    query.prepare( "SELECT DISTINCT hostname from settings where hostname is not null;");

    if (query.exec() && query.isActive() && query.size() > 0)
    {
        while (query.next())
        {
            tempItem = query.value(0).toString();
            gc->addSelection(tempItem);

        }
    }
    gc->setHelpText(QObject::tr("Select the Host you wish to copy settings from."));

    return gc;
}

class TemplateSettings:
    public TriggeredConfigurationGroup {
public:
    TemplateSettings():
        //ConfigurationGroup(false, true, false, false),
        //VerticalConfigurationGroup(false, true, false, false),
        TriggeredConfigurationGroup(true) {
        setLabel(QObject::tr("Manage templates"));
//         setUseLabel(false);

        Setting* Mtemplate = HOSTtemplatetype();
        addChild(Mtemplate);
        setTrigger(Mtemplate);


        ConfigurationGroup* trestore = new VerticalConfigurationGroup(false);
        trestore->addChild(HostTemplateRestore());

        ConfigurationGroup* tsave = new VerticalConfigurationGroup(false);
        tsave->addChild(HostTemplateSave());

        ConfigurationGroup* tcopy = new VerticalConfigurationGroup(false);
        tcopy->addChild(HostSelectcopy_from());
        tcopy->addChild(HostTemplateCopy_restore());


        addTarget("Do Nothing", new VerticalConfigurationGroup(true));
        addTarget("restore", trestore);
        addTarget("save", tsave);
        addTarget("copy_from", tcopy);

    };
};

MythCtemplate::MythCtemplate()
{

    TemplateSettings *templatesettings = new TemplateSettings();
    addChild(templatesettings);

}