summaryrefslogtreecommitdiff
path: root/zaplist.c
blob: afb3ce0ffa8b6524b629ba232975533ba0bca0aa (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
/*
 * zaplist.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include <vdr/channels.h>

#include "zaplist.h"
#include "setup-zaphistory.h"

cZapHistory ZapHistory;

#if APIVERSNUM >= 20301
cZapHistoryChannel* cZapHistory::FindChannel(const cChannel *channel) {
#else
cZapHistoryChannel* cZapHistory::FindChannel(cChannel *channel) {
#endif
    if (channel == NULL)
	    return NULL;

    cZapHistoryChannel *returnVal = NULL;

    // Look for channel
    for ( cZapHistoryChannel *zapChan = First(); zapChan; zapChan = Next( zapChan ) ) {
	    if ( zapChan->GetChannelID() == channel->GetChannelID() ) {
	        returnVal = zapChan;
	        break;
	    }
    }

    return returnVal;
}

void cZapHistory::Reset( cZapHistoryChannel *zapChan, bool statisticOnly ) {
    if (zapChan != NULL) { // Operate on ONE channel
	    if (statisticOnly) { // reset statistics
	        zapChan->Reset();
	    } else { // delete entry
	        if (zapChan == currentChannel)
		        currentChannel = NULL;
	        Del(zapChan);
	    }
    } else { // operate in complete history
	    if (statisticOnly) { // reset statistics
	        for ( cZapHistoryChannel *zapChan = First(); zapChan; zapChan = Next( zapChan ) ) {
		        zapChan->Reset();
	        }
	    } else { // clear history
	        currentChannel = NULL;

	        while (First() != NULL) {
		        Del( First() );
	        }
	    }
    }

    Save();
}

void cZapHistory::UpdateHistory(cZapHistoryChannel *zapChan) {
    for ( cZapHistoryChannel *chan = First(); chan; chan = Next( chan ) ) {
	    chan->SetHistoryPos( chan->GetHistoryPos() + 1);
    }

    zapChan->IncreaseZapCount();
    zapChan->SetHistoryPos(1);
    UpdateWatchTime();
}

cZapHistory::cZapHistory() {
    currentChannel = NULL;
    lastSwitch = time(NULL);
    sortMode = historySort;
}

void cZapHistory::ViewInterrupted() {
    // Add view time to current channel before playback of st. is started
    UpdateWatchTime();

    lastSwitch = -1;
}

cZapHistorySortMode cZapHistory::GetSortMode() {
    return sortMode;
}

void cZapHistory::SetSortMode( cZapHistorySortMode mode ) {
    sortMode = mode;
}

void cZapHistory::UpdateWatchTime() {
    if (currentChannel == NULL || lastSwitch == -1)
	    return;

    // Add wiew time since last switch
    currentChannel->AddWatchTime( time(NULL) - lastSwitch );

    lastSwitch = time(NULL);
}

void cZapHistory::ViewContinue() {
    lastSwitch = time(NULL);
}
#if APIVERSNUM >= 20301
void cZapHistory::ChannelSwitch( const cChannel *channel ) {
#else
void cZapHistory::ChannelSwitch( cChannel *channel ) {
#endif
    cZapHistoryChannel *zapChan = FindChannel( channel );

    // Channel not in history management
    if ( zapChan == NULL ) {
	    // Create new history channel
	    zapChan = new cZapHistoryChannel();
	    zapChan->SetChannelID( channel->GetChannelID() );
	    // Add channel to history
	    Ins( zapChan );
	    UpdateHistory(zapChan);
    } else if (currentChannel != zapChan) { // Channel already in history management and
	    // a real switch (another channel)	
	    UpdateHistory(zapChan);
    }

    currentChannel = zapChan;

    // Write history to file
    ZapHistory.Save();
}