summaryrefslogtreecommitdiff
path: root/channelgroups.c
blob: 5069795e7f464d8e4ec8b643d7028048999737ec (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
#include "config.h"
#include "channelgroups.h"

// --- cChannelGroup  -------------------------------------------------------------

cChannelGroup::cChannelGroup(string name, int id) {
    this->id = id;
    channelStart = -1;
    channelStop = -1;
    this->name = name;
}

cChannelGroup::~cChannelGroup(void) {
}

void cChannelGroup::Debug(void) {
    dsyslog("tvguideng: id %d channel group %s, start %d, stop %d", id, name.c_str(), channelStart, channelStop);
}

// --- cChannelgroups  -------------------------------------------------------------

cChannelgroups::cChannelgroups(skindesignerapi::cViewGrid *channelgroupGrid) {
    this->channelgroupGrid = channelgroupGrid;
}

cChannelgroups::~cChannelgroups(void) {
    Clear();
}

void cChannelgroups::Init(void) {
    bool setStart = false;
    int lastChannelNumber = 0;
    int id = 0;
    const cChannel *first = Channels.First();
    if (!first->GroupSep()) {
        channelGroups.push_back(cChannelGroup(tr("Main Program"), id++));
        setStart = true;
    }    
    for (const cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel)) {
        if (setStart && (channelGroups.size() > 0)) {
            channelGroups[channelGroups.size()-1].SetChannelStart(channel->Number());
            setStart = false;
        }
        if (channel->GroupSep()) {
            if (channelGroups.size() > 0) {
                channelGroups[channelGroups.size()-1].SetChannelStop(lastChannelNumber);
            }
            channelGroups.push_back(cChannelGroup(channel->Name(), id++));
            setStart = true;
        } else {
            lastChannelNumber = channel->Number();
        }
    }
    if (channelGroups.size() > 0) {
        channelGroups[channelGroups.size()-1].SetChannelStop(lastChannelNumber);
    }
    //cleanup empty groups
    bool foundDelete = false;
    do {
        for (vector<cChannelGroup>::iterator it = channelGroups.begin(); it != channelGroups.end(); it++) {
            cChannelGroup group = *it;
            if (group.StartChannel() == 0 || group.StartChannel() == -1) {
                channelGroups.erase(it);
                foundDelete = true;
                break;
            }
            foundDelete = false;
        }
    } while (foundDelete);
}

void cChannelgroups::Clear(void) {
    channelgroupGrid->Clear();
}

void cChannelgroups::Draw(const cChannel *start, const cChannel *stop) {
    if (!start || !stop)
        return;
    
    int group = GetGroup(start);
    int groupLast = group;
    int fields = 1;
    double offset = 0.0;

    for (const cChannel *channel = Channels.Next(start); channel; channel = Channels.Next(channel)) {
        if (channel->GroupSep())
            continue;
        if (config.hideLastChannelGroup && IsInLastGroup(channel)) {
            SetGroup(group, fields, offset);
            break;            
        }
        group = GetGroup(channel);
        if (group != groupLast) {
            offset = SetGroup(groupLast, fields, offset);
            fields = 0;
        }
        fields++;
        groupLast = group;
        if (channel == stop) {
            SetGroup(group, fields, offset);
            break;
        }
    }
    channelgroupGrid->Display();
}

double cChannelgroups::SetGroup(int groupId, int fields, double offset) {
    int channelsPerPage;
    double x, y, width, height;
    if (config.displayMode == eHorizontal) {
        channelsPerPage = config.channelsPerPageHorizontal;
        x = 0.0;
        y = offset;
        width = 1.0;
        height = (double)fields / (double)channelsPerPage;
        offset += height;
    } else {
        channelsPerPage = config.channelsPerPageVertical;
        x = offset;
        y = 0.0;
        width = (double)fields / (double)channelsPerPage;
        height = 1.0;
        offset += width;
    }
    string groupName = channelGroups[groupId].GetName();
    channelgroupGrid->ClearTokens();
    channelgroupGrid->AddIntToken("color", groupId % 2);
    channelgroupGrid->AddStringToken("group", groupName);
    channelgroupGrid->SetGrid(groupId, x, y, width, height);

    return offset;
}

int cChannelgroups::GetGroup(const cChannel *channel) {
    if (!channel)
        return -1;
    int channelNumber = channel->Number();
    for (vector<cChannelGroup>::iterator group = channelGroups.begin(); group != channelGroups.end(); group++) {
        if ( (*group).StartChannel() <= channelNumber && (*group).StopChannel() >= channelNumber ) {
            return (*group).GetId();
        }
    }
    return -1;
}

string cChannelgroups::GetPrevGroupName(int group) {
    if (group <= 0 || group > (int)channelGroups.size())
        return "";
    return channelGroups[group-1].GetName();
}

string cChannelgroups::GetNextGroupName(int group) {
    if (group < 0 || group >= (int)channelGroups.size() - 1)
        return "";
    return channelGroups[group+1].GetName();
}

int cChannelgroups::GetPrevGroupFirstChannel(int group) {
    if (group <= 0 || group > (int)channelGroups.size())
        return -1;
    return channelGroups[group-1].StartChannel();
}

int cChannelgroups::GetNextGroupFirstChannel(int group) {
    if (group < 0 || group >= (int)channelGroups.size())
        return -1;
    return channelGroups[group+1].StartChannel();
}

bool cChannelgroups::IsInFirstGroup(const cChannel *channel) {
    if (channelGroups.size() == 0)
        return false;
    int channelNumber = channel->Number();
    if (channelNumber >= channelGroups[0].StartChannel() && channelNumber <= channelGroups[0].StopChannel())
        return true;
    return false;
}

bool cChannelgroups::IsInLastGroup(const cChannel *channel) {
    size_t numGroups = channelGroups.size();
    if (numGroups == 0)
        return false;
    int channelNumber = channel->Number();
    if (channelNumber >= channelGroups[numGroups-1].StartChannel() && channelNumber <= channelGroups[numGroups-1].StopChannel()) {
        return true;
    }
    return false;
}

bool cChannelgroups::IsInSecondLastGroup(const cChannel *channel) {
    size_t numGroups = channelGroups.size();
    if (numGroups < 2)
        return false;
    int channelNumber = channel->Number();
    if (channelNumber >= channelGroups[numGroups-2].StartChannel() && channelNumber <= channelGroups[numGroups-2].StopChannel())
        return true;
    return false;
}

int cChannelgroups::GetLastValidChannel(void) {
    if (config.hideLastChannelGroup && channelGroups.size() > 1) {
        return channelGroups[channelGroups.size()-2].StopChannel();
    }
    return Channels.MaxNumber();
}