summaryrefslogtreecommitdiff
path: root/mngurls.c
blob: 44361a944854f9b664eafa4d7a44bb9d65511f3f (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
/*
 * mgnurls.c: VDR on Smart TV plugin
 *
 * Copyright (C) 2012, 2013 T. Lohmar
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 * Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 *
 */

#include "mngurls.h"


cManageUrls::cManageUrls(string dir): mLog(), mFilename(), mFile(NULL), mEntries() {
  mLog = Log::getInstance();

  loadEntries(dir);
  mFilename = dir +"/urls.txt"; 
  mFile = new ofstream(mFilename.c_str(), ios::out | ios::app);  
  mFile->seekp(ios_base::end);
};

cManageUrls::~cManageUrls() {
  if (mFile != NULL) {
    mFile->close();
    delete mFile;
  }
  //TODO: delete entries
};


//called from outside to add an entry
void cManageUrls::appendEntry(string type, string url) {
  // iter through entries
  *(mLog->log()) << " cManageUrls::appendEntry: type= " << type << "url= " << url << endl;

  bool found = false;
  if (type.compare("YT") !=0) {
    return;
  }
  for (size_t i = 0; i < mEntries.size(); i ++) {
    if (url.compare(mEntries[i]->mEntry) == 0) {
      found = true;
      break;
    }
  }
  if (!found) {
    *(mLog->log()) << " cManageUrls::appendEntry: Appending... " << endl;
    mEntries.push_back (new sUrlEntry (type, url));
    appendToFile(type+"|"+url);
  }
}


bool cManageUrls::deleteEntry(string type, string url) {
  *(mLog->log()) << " cManageUrls::deleteEntry: type= " << type << "guid= " << url << endl;

  bool found = false;
  if (type.compare("YT") !=0) {
    *(mLog->log()) << " cManageUrls::deleteEntry: Not a YT Url "  << endl;
    return false;
  }
  
  for (size_t i = 0; i < mEntries.size(); i ++) {
    if (url.compare(mEntries[i]->mEntry) == 0) {
      // delete the entry here
      *(mLog->log()) << " cManageUrls::deleteEntry ... " << endl;
      mEntries.erase(mEntries.begin() +i);
      found = true;
      break;
    }
  }

  if (found) {
    *(mLog->log()) << " cManageUrls::deleteEntry - rewriting urls.txt file ... " << endl;
    if (mFile != NULL) {
      mFile->close();
      delete mFile;
    }
    mFile = new ofstream(mFilename.c_str(), ios::out);  
    
    for (size_t i = 0; i < mEntries.size(); i ++) {
      appendToFile(mEntries[i]->mType+"|"+mEntries[i]->mEntry);
    }

    // close the file
    mFile->close();
    delete mFile;
    
    // open for append
    mFile = new ofstream(mFilename.c_str(), ios::out | ios::app);  
    mFile->seekp(ios_base::end);
  }
  return found;
}



size_t cManageUrls::size() { 
  return mEntries.size(); 
} 

sUrlEntry* cManageUrls::getEntry( int index)  { 
  return mEntries[index]; 
};

void cManageUrls::loadEntries(string dir) {
  ifstream myfile ((dir +"/urls.txt").c_str());
  string line;

  string type;

  while ( myfile.good() ) {
    getline (myfile, line);

    if ((line == "") or (line[0] == '#'))
      continue;

    size_t pos = line.find('|');
    string type = line.substr(0, pos);
    string value = line.substr(pos+1);

    //    sUrlEntry* entry = new sUrlEntry(type, value);
    mEntries.push_back(new sUrlEntry(type, value));
  }
  myfile.close();
};

void cManageUrls::appendToFile(string s_line) {
  if (mFile == NULL) {
    *(mLog->log()) << " ERROR in cManageUrls::appendToFile: no file open... " << endl;
    return;
  }
  *(mLog->log()) << " cManageUrls::appendToFile: writing  " << s_line << endl;
  *mFile << s_line << endl;
  //  mFile->write(s_line.c_str(), s_line.size());

  mFile->flush();

}