summaryrefslogtreecommitdiff
path: root/logger.c
blob: f60834f9533c0c22461156df0575b64d95786495 (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
/*
 * logger.c: Keep log of replayed recordings
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include "logger.h"
#include <string.h>

cHistoryRecordingItem::cHistoryRecordingItem(const char *String)
{
  filename = NULL;
  name = NULL;
  if (String && *String) {
     char *s = strdup(String);
     char *p = strchr(s, ':');
     if (p) {
        replay_time = atol(s);
        char *r = strchr(p, '=');
        if (r) {
           *r = 0;
           filename = strdup(p + 1);
           name = strdup(r + 1);
           }
        else
           filename = strdup(p + 1);
        }
     else {
        replay_time = 0;
        }
     free(s);
     }
}

// Use name only if not a VDR recording
cHistoryRecordingItem::cHistoryRecordingItem(const char *Name, const char *FileName)
{
  replay_time = time(NULL);
  filename = FileName ? strdup(FileName) : NULL;
  name = Name ? strdup(Name) : NULL;
}

char *cHistoryRecordingItem::GetReplayTimeString()
{
  struct tm *t = localtime(&replay_time);
  char *time;
  if (asprintf(&time, "%02d.%02d.%02d %02d:%02d",
                      t->tm_mday, t->tm_mon + 1, t->tm_year % 100,
                      t->tm_hour, t->tm_min))
     return time;
  else
     return NULL;
}

cHistoryLogger::cHistoryLogger()
{
  replay_history_filename = NULL;
  replay_history = new cList<cHistoryRecordingItem>();
}

cHistoryLogger::~cHistoryLogger()
{
  free(replay_history_filename);
  replay_history->Clear();
  delete replay_history;
}

void cHistoryLogger::Replaying(const cControl *Control, const char *Name, const char *FileName, bool On)
{
  if (On) {
     if (FileName) {
        // Check if file is a VDR recording
        const cRecording *recording;
#if VDRVERSNUM >= 20301
        {
        LOCK_RECORDINGS_READ;
        recording = Recordings->GetByName(FileName);
        }
#else
        cThreadLock RecordingsLock(&Recordings);
        recording = Recordings.GetByName(FileName);
#endif
        // Use name only if not a VDR recording
        replay_history->Ins(new cHistoryRecordingItem(!recording ? Name : NULL, FileName));
        if (replay_history->Count() > HistorySetup.replay_history_size)
           replay_history->Del(replay_history->Last());
        StoreReplayHistory();
        }
     }
}

void cHistoryLogger::DeleteRecordingItem(int index)
{
  if (index >= 0 && index < replay_history->Count()) {
     replay_history->Del(replay_history->Get(index));
     StoreReplayHistory();
     }
}

cHistoryRecordingItem *cHistoryLogger::GetRecordingItem(int index)
{
  if (index >= 0 && index < replay_history->Count())
     return replay_history->Get(index);
  return NULL;
}

cList<cHistoryRecordingItem> *cHistoryLogger::GetReplayHistory()
{
  return replay_history;
}

void cHistoryLogger::ClearReplayHistory()
{
  replay_history->Clear();
  if (*replay_history_filename) {
     FILE *f = fopen(replay_history_filename, "w");
     if (f) {
        fclose(f);
        }
     else {
        LOG_ERROR_STR(replay_history_filename);
        }
     }
}

void cHistoryLogger::LoadReplayHistory()
{
  if (*replay_history_filename && access(replay_history_filename, F_OK) == 0) {
     FILE *f = fopen(replay_history_filename, "r");
     if (f) {
        char *s;
        int line = 0;
        cReadLine ReadLine;
        while ((s = ReadLine.Read(f)) != NULL) {
              ++line;
              if (!isempty(s)) {
                 replay_history->Add(new cHistoryRecordingItem(s));
                 }
              }
        fclose(f);
        }
     else {
        LOG_ERROR_STR(replay_history_filename);
        }
     }
}

void cHistoryLogger::StoreReplayHistory()
{
  if (*replay_history_filename) {
     FILE *f = fopen(replay_history_filename, "w");
     if (f) {
        cHistoryRecordingItem *item = replay_history->First();
        while (item != NULL) {
              if (item->GetName())
                 fprintf(f, "%li:%s=%s\n", *(item->GetReplayTime()), item->GetFilename(), item->GetName());
              else
                 fprintf(f, "%li:%s\n", *(item->GetReplayTime()), item->GetFilename());
              item = (cHistoryRecordingItem *)item->Next();
              }
        fclose(f);
        }
     else {
        LOG_ERROR_STR(replay_history_filename);
        }
     }
}

void cHistoryLogger::SetReplayHistoryConfigFile(const char *FileName)
{
  free(replay_history_filename);
  replay_history_filename = strdup(FileName);
}