summaryrefslogtreecommitdiff
path: root/misc/menusetup.cpp
blob: 9e8386f048c9002f95c9ef8c5824bc876b5dd5c7 (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
/* 
 * File:   menusetup.cpp
 * Author: savop
 * 
 * Created on 19. April 2009, 16:50
 */

#include "config.h"
#include <vdr/osdbase.h>
#include "menusetup.h"
#include "../common.h"
#include "util.h"
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <vdr/menuitems.h>


cMenuSetupUPnP::cMenuSetupUPnP(){
    // Get server acitve state
    MESSAGE("Creating menu");
    this->mCtrlBind = NULL;
    this->mCtrlAutoMode = NULL;
    this->mCtrlEnabled = NULL;
    this->mCtrlPort = NULL;
    this->mEnable = 0;
    this->mDetectPort = 0;
    this->mAutoSetup = 0;
    this->mPort = 0;
    this->mAddress = NULL;
    this->mInterfaceIndex = 0;
    this->Load();
    this->Update();
}

//cMenuSetupUPnP::~cMenuSetupUPnP() {
//    delete this->mCtrlAutoMode;
//    delete this->mCtrlEnabled;
//    delete this->mCtrlPort;
//    free(this->mAddress);
//}

void cMenuSetupUPnP::Load(void){
    cUPnPConfig* Config = cUPnPConfig::get();
    this->mEnable = Config->mEnable;
    this->mAutoSetup = Config->mAutoSetup;
    this->mInterfaceIndex = this->getInterfaceIndex(Config->mInterface);
    this->mAddress = strdup(Config->mAddress?Config->mAddress:"0.0.0.0");
    this->mPort = Config->mPort;

    if(Config->mPort==0) this->mDetectPort = 1;
}

const char* const* cMenuSetupUPnP::getInterfaceList(int* count){
    char** Ifaces = getNetworkInterfaces(count);
    char** IfaceList = new char*[++(*count)];
    IfaceList[0] = strdup(_("User defined"));
    for(int i=0; i < *count-1; i++){
        IfaceList[i+1] = strdup(Ifaces[i]);
    }
    delete [] Ifaces;
    return IfaceList;
}

int cMenuSetupUPnP::getInterfaceIndex(const char* Interface){
    MESSAGE("Getting Index of %s", Interface);
    if(!Interface) return 0;
    int count;
    int Index = 0;
    const char* const* Ifaces = this->getInterfaceList(&count);

    for(int i=1; i < count; i++){
        if(!strcmp(Interface, Ifaces[i])){
            Index = i;
            break;
        }
    }
    delete [] Ifaces;
    return Index;
}

const char* cMenuSetupUPnP::getInterface(int Index){
    int count;
    const char* const* Ifaces = this->getInterfaceList(&count);

    if(count < Index || Index < 1) return NULL;
    const char* Interface = strdup0(Ifaces[Index]);
    delete [] Ifaces;
    return Interface;
}

void cMenuSetupUPnP::Update(void){
    int Current = this->Current();
    this->Clear();
    // Add OSD menu item for enabling UPnP Server
    this->Add(mCtrlEnabled = new cMenuEditBoolItem(_("Enable UPnP Server"),&this->mEnable,_("disabled"),_("enabled")));
    if(this->mEnable){
        cMenuEditIntItem* editPortItem = NULL;
        this->Add(mCtrlAutoMode = new cMenuEditBoolItem(_("Auto detect settings"),&this->mAutoSetup,_("no"),_("yes")));
        // Add OSD menu item for IP address
        int Count;
        const char* const* Interfaces = this->getInterfaceList(&Count);
        this->Add(mCtrlBind = new cMenuEditStraItem(_("Bind to network interface"), &this->mInterfaceIndex, Count, Interfaces));
        
        cMenuEditIpItem* editIpItem;
        if(this->mInterfaceIndex){
            const sockaddr_in* addr = getIPFromInterface(this->getInterface(this->mInterfaceIndex));
            char* IP = strdup(inet_ntoa(addr->sin_addr));
            editIpItem = new cMenuEditIpItem(_("Current IP address"),IP);
            editIpItem->SetSelectable(false);
            free(IP);
        }
        else {
            editIpItem = new cMenuEditIpItem(_("Set IP address"),this->mAddress);
        }
        this->Add(editIpItem);
        this->Add(mCtrlPort = new cMenuEditBoolItem(_("Select port"), &this->mDetectPort, _("auto"), _("user definied")));
        if(this->mDetectPort){
            this->Add(editPortItem = new cMenuEditIntItem(_("User specified port"),
                                                   &this->mPort,
                                                   SERVER_MIN_PORT,
                                                   SERVER_MAX_PORT
                                                  ));
        }
        
        if(this->mAutoSetup){
            if(mCtrlPort) mCtrlPort->SetSelectable(false);
            if(mCtrlBind) mCtrlBind->SetSelectable(false);
            if(editPortItem) editPortItem->SetSelectable(false);
            if(editIpItem) editIpItem->SetSelectable(false);
        }
        else {
            if(mCtrlPort) mCtrlPort->SetSelectable(true);
            if(mCtrlBind) mCtrlBind->SetSelectable(true);
            if(editPortItem) editPortItem->SetSelectable(true);
            if(editIpItem && !this->mInterfaceIndex) editIpItem->SetSelectable(true);
        }
    }
    this->SetCurrent(this->Get(Current));
    this->Display();
}

eOSState cMenuSetupUPnP::ProcessKey(eKeys Key){

    cOsdItem *Item = this->Get(this->Current());

    eOSState State = cMenuSetupPage::ProcessKey(Key);

    Key = NORMALKEY(Key);

    if(Key != kRight && Key != kLeft){
        return State;
    }
    
    if(Item == this->mCtrlEnabled){
        if(this->mEnable){
            this->Update();
        }
        else if (!this->mEnable) {
            this->Update();
        }
    }
    else if (Item == this->mCtrlPort){
        if(this->mDetectPort){
            this->Update();
        }
        else if(!this->mDetectPort) {
            this->Update();
        }
    }
    else if (Item == this->mCtrlAutoMode){
        if(this->mAutoSetup){
            this->Update();
        }
        else if(!this->mAutoSetup) {
            this->Update();
        }
    }
    else if(Item == this->mCtrlBind){
//        if(!this->mInterfaceIndex){
//            this->Update();
//        }
//        else if(!this->mInterfaceIndex){
//            this->Update();
//        }
        this->Update();
    }
    return State;
}

void cMenuSetupUPnP::Store(void){
    cUPnPConfig* Config = cUPnPConfig::get();
    Config->mAddress = strdup0(this->mAddress);
    Config->mAutoSetup = this->mAutoSetup;
    Config->mEnable = this->mEnable;
    Config->mInterface = strdup0(this->getInterface(this->mInterfaceIndex));
    Config->mPort = (this->mDetectPort) ? 0 : this->mPort;

    this->SetupStore(SETUP_SERVER_AUTO, this->mAutoSetup);
    this->SetupStore(SETUP_SERVER_ENABLED, this->mEnable);
    this->SetupStore(SETUP_SERVER_INT, this->getInterface(this->mInterfaceIndex));
    this->SetupStore(SETUP_SERVER_ADDRESS, this->mAddress);
    this->SetupStore(SETUP_SERVER_PORT, this->mPort);
    
}