summaryrefslogtreecommitdiffstats
path: root/abs/core-testing/mythinstall/autocard.cpp
blob: 585fba77cce9a3ad2e68194899440ca34c83bcbd (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
#include "libmyth/mythcontext.h"
#include "libmyth/mythdbcon.h"
#include <qsqldatabase.h>
#include <qheader.h>
#include <qcursor.h>
#include <qlayout.h>
#include <iostream>
#include "autocard.h"
#include <stdlib.h>
#include <cstdlib>

AutoCard::AutoCard(void) :
    listbox(new ListBoxSetting(this))
{
    listbox->setLabel(tr("Cards"));
    addChild(listbox);
}
void AutoCard::popup_menu(void)
{
    QString name = listbox->getValue();
//     cout << "in popup" <<  name << endl;
    QString uniqid =  name.section(' ', -1);
    QString description = name.section('*',-3,-3);
    description = description.section('.',1);
    description = description.stripWhiteSpace();
//     cout << uniqid << endl;
//     cout << "descriptn" << endl;
//     cout << description << endl;
    if (name.isEmpty() || name == "Default")
        return;

    QString message = tr("Cardme:") +
        QString("\n'%1'?").arg(name);

    DialogCode  value = MythPopupBox::Show2ButtonPopup(gContext->GetMainWindow(),
                                               "", message,
                                               tr("Add Card"),
                                               tr("Do not Add"), kDialogCodeButton0);

    if ( value == kDialogCodeButton1)
    {
        MSqlQuery query(MSqlQuery::InitCon());
        query.prepare("update autocard set devicestatus ='unused' where uniqid=:UNIQID and description=:DESCRIPTION;");
         query.bindValue(":UNIQID",uniqid );
        query.bindValue(":DESCRIPTION",description );
        if (!query.exec())
            MythContext::DBError("Autocard::popup", query);

        int lastIndex = listbox->getValueIndex(name);
        lastValue = "";
        load();
        listbox->setValue(lastIndex);
    }
    else if (value == kDialogCodeButton0)
    {
        MSqlQuery query(MSqlQuery::InitCon());
        query.prepare("update autocard set devicestatus ='will-add' where uniqid=:UNIQID and description=:DESCRIPTION;");
        query.bindValue(":UNIQID",uniqid );
        query.bindValue(":DESCRIPTION",description );


        if (!query.exec())
            MythContext::DBError("Autocard::popup", query);

        int lastIndex = listbox->getValueIndex(name);
        lastValue = "";
        load();
        listbox->setValue(lastIndex);




    }


    listbox->setFocus();
}



QStringList AutoCard::GetNames(void)
{
    QStringList names;
    QString device;
    QString description;
    QString displayname ;
    QString status ;
    QString uniqid;
    MSqlQuery query(MSqlQuery::InitCon());
    query.prepare("SELECT dev,description,uniqid,devicestatus FROM autocard  order by uniqid");
    if (query.exec() && query.isActive() && query.size() > 0)
    {
       while (query.next())
       {
            device=query.value(0).toString();
            description=query.value(1).toString();
            uniqid=query.value(2).toString();
            status=query.value(3).toString();
            //displayname=device;
            displayname=".     ";
            displayname.append(description);
            displayname.append(" * ");
            displayname.append(status);
            displayname.append(" * ");

            displayname.append(uniqid);
            names << displayname  ;
       }

    return names;
    }
}

void AutoCard::open(QString name)
{
    lastValue = name;
    bool created = false;
    cout << name << endl;
    if (name == "Mark all cards for addition")
    {
       cout <<"updating all cards" << endl;
       MSqlQuery query(MSqlQuery::InitCon());
       query.prepare("update autocard set devicestatus='will-add' where devicestatus='unused' order by uniqid");
       query.exec();
    }
    else if ( name == "Perform actions" )
    {
        // write udev rules and reload udev

       system ("sudo autocard.py -w -i");
       system ("sudo autocard.py -r");

    }
    else if ( name == "Exit" )
    {
        // write udev rules and reload udev
        exit(0);
    }
    else
    {
    popup_menu();
// bool ok = MythPopupBox::showGetTextPopup(gContext->GetMainWindow(),
//             tr("Create New Playback Group"),
//             tr("Enter group name or press SELECT to enter text via the "
//                "On Screen Keyboard"), name);


    }

};

void AutoCard::load(void)
{
    listbox->clearSelections();


    QStringList names = AutoCard::GetNames();
    while (!names.isEmpty())
    {
        listbox->addSelection(names.front());
        names.pop_front();
    }
    listbox->addSelection("Mark all cards for addition");
    listbox->addSelection("Perform actions");
    listbox->addSelection("Exit");
    listbox->setValue(lastValue);

}


int AutoCard::exec(void)
{
    while (ConfigurationDialog::exec() == QDialog::Accepted)
        open(listbox->getValue());

    return QDialog::Rejected;
}


MythDialog* AutoCard::dialogWidget(MythMainWindow* parent,
                                          const char* widgetName)
{
    dialog = ConfigurationDialog::dialogWidget(parent, widgetName);
    connect(dialog, SIGNAL(menuButtonPressed()), this, SLOT(popup_menu()));
    return dialog;
}