summaryrefslogtreecommitdiff
path: root/zaphistorychannel.c
blob: dc730a0caa90dde967e286d881dce8f0872ff825 (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
/*
 * zaphistorychannel.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"
#include "zaphistorychannel.h"

cZapHistoryChannel::cZapHistoryChannel() {
    // init members
    //channelID = tChannelID.tChannelID();
    watchTime = 0;
    zapCount = 0;
}

// Getter/Setter for wrapped cChannel
const cChannel *cZapHistoryChannel::GetChannel() {
#if APIVERSNUM >= 20301
    LOCK_CHANNELS_READ;
    return Channels->GetByChannelID(channelID);
#else
    return Channels.GetByChannelID(channelID);
#endif
}

tChannelID cZapHistoryChannel::GetChannelID() {
    return channelID;
}

void cZapHistoryChannel::SetChannelID(tChannelID id) {
    channelID = id;
}

// Reset statistics
void cZapHistoryChannel::Reset() {
    watchTime = 0;
    zapCount = 0;
}

// Getter for statistics
time_t cZapHistoryChannel::GetWatchTime() {
    return watchTime;
}

// Getter for statistics
long cZapHistoryChannel::GetZapCount() {
    return zapCount;
}

// Increases zap count
void cZapHistoryChannel::IncreaseZapCount() {
    zapCount++;
}

// Increases watch time
void cZapHistoryChannel::AddWatchTime( time_t t) {
    watchTime += t;
}

// Getter for position in history
int cZapHistoryChannel::GetHistoryPos() {
    return historyPos;
}

// Setter for position in history
void cZapHistoryChannel::SetHistoryPos(int p) {
    historyPos = p;
}

// Compares this instance to another cZapHistoryChannel
int cZapHistoryChannel::Compare(const cListObject &ListObject) const {
    // Comparison depends on current sort mode of history
    switch( ZapHistory.GetSortMode() ) {
	case historySort:
	    if ( historyPos == ((cZapHistoryChannel&) ListObject).GetHistoryPos() )
		return 0;
	    if ( historyPos > ((cZapHistoryChannel&) ListObject).GetHistoryPos() )
		return 1;
	
	    return -1;
	case zapcountSort:
	    if ( zapCount == ((cZapHistoryChannel&) ListObject).GetZapCount() )
		return 0;
	    if ( zapCount > ((cZapHistoryChannel&) ListObject).GetZapCount() )
		return -1;
	
	    return 1;
	case watchtimeSort:
	    if ( watchTime == ((cZapHistoryChannel&) ListObject).GetWatchTime() )
		return 0;
	    if ( watchTime > ((cZapHistoryChannel&) ListObject).GetWatchTime() )
		return -1;
	
	    return 1;
    }
    
    return 0;
}

bool cZapHistoryChannel::Parse( char *s ) {
    char *channelbuffer = NULL;
    
    // Parse line from zaphistory.conf
    if ( sscanf( s, "%a[^:]:%d:%ld:%ld", &channelbuffer, &historyPos, &zapCount, &watchTime) == 4) {
	// get channel ID
	channelID = tChannelID::FromString(channelbuffer);							
	// free buffer
	free(channelbuffer);

	// if invalid channel do not add to history
	if (GetChannel() == NULL)
	    return false;	

	// parsed successful	
	return true;	
    }
    
    // could not parse
    return false;
}

bool cZapHistoryChannel::Save( FILE *f ) {
    return fprintf(f, "%s:%d:%ld:%ld\n", *channelID.ToString(), GetHistoryPos(), GetZapCount(), GetWatchTime()  );
}