~ubuntu-branches/ubuntu/precise/gmlive/precise

« back to all changes in this revision

Viewing changes to src/recentchannel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Aron Xu
  • Date: 2010-01-28 10:02:52 UTC
  • Revision ID: james.westby@ubuntu.com-20100128100252-b0yb0n4zm7s85ce7
Tags: upstream-0.22.2
Import upstream version 0.22.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * =====================================================================================
 
3
 *
 
4
 *       Filename:  recentchannel.cpp
 
5
 *
 
6
 *    Description:  
 
7
 *
 
8
 *        Version:  1.0
 
9
 *        Created:  2007年12月02日 20时19分58秒 CST
 
10
 *       Revision:  none
 
11
 *       Compiler:  gcc
 
12
 *
 
13
 *         Author:  lerosua (), lerosua@gmail.com
 
14
 *        Company:  Cyclone
 
15
 *
 
16
 * =====================================================================================
 
17
 */
 
18
 
 
19
#include <stdlib.h>
 
20
#include <fstream>
 
21
#include <iostream>
 
22
#include <vector>
 
23
#include "recentchannel.h"
 
24
#include "MainWindow.h"
 
25
#include "mmsLivePlayer.h"
 
26
#include "sopcastLivePlayer.h"
 
27
#include "pplivePlayer.h"
 
28
#include "ppsPlayer.h"
 
29
 
 
30
RecentChannel::RecentChannel(MainWindow* parent_):Channel( parent_)
 
31
{
 
32
        init();
 
33
}
 
34
 
 
35
RecentChannel::~RecentChannel()
 
36
{}
 
37
 
 
38
LivePlayer* RecentChannel::get_player( const std::string& stream,TypeChannel page)
 
39
{
 
40
        switch(page) {
 
41
                case MMS_CHANNEL:
 
42
                        return MmsLivePlayer::create(stream);
 
43
                case PPLIVE_CHANNEL:
 
44
                        return PpLivePlayer::create(stream);
 
45
                case SOPCAST_CHANNEL:
 
46
                        return SopcastLivePlayer::create(stream);
 
47
                case PPS_CHANNEL:
 
48
                        return PPSPlayer::create(stream);
 
49
                default:
 
50
                        g_warn_if_reached();
 
51
                        return NULL;
 
52
        }
 
53
}
 
54
void RecentChannel::init()
 
55
{
 
56
        char buf[512];
 
57
        std::string homedir=Glib::get_user_config_dir();
 
58
        snprintf(buf, 512,"%s/gmlive/recent.lst",homedir.c_str());
 
59
        std::ifstream file(buf);
 
60
        if(!file){
 
61
                std::ofstream out(buf);
 
62
                out.close();
 
63
                file.open(buf);
 
64
                //std::cout<<"file error\n";
 
65
                //return;
 
66
        }
 
67
        std::string line;
 
68
        std::string last;
 
69
        std::string name;
 
70
        std::string stream;
 
71
        std::string type;
 
72
        std::string trimstring = "\t";
 
73
        if(file){
 
74
                while(std::getline(file,line)){
 
75
                        size_t pos = line.find_first_of("#");
 
76
                        if(pos==std::string::npos)
 
77
                                continue;
 
78
                        name = line.substr(0,pos);
 
79
                        last = line.substr(pos+1,std::string::npos);
 
80
 
 
81
                        pos = last.find_first_of("#");
 
82
                        if(pos==std::string::npos)
 
83
                                continue;
 
84
                        //stream=last.substr(0,pos);
 
85
                        stream=last.substr(0,last.find_first_of(trimstring));
 
86
                        type= last.substr(pos+1,std::string::npos);
 
87
                        
 
88
                        int id=0;
 
89
                        addLine(id,name,stream,type);
 
90
                }
 
91
        }
 
92
 
 
93
        file.close();
 
94
 
 
95
}
 
96
 
 
97
void  RecentChannel::addLine(int num,const Glib::ustring& name,const std::string& stream_,const Glib::ustring& type)
 
98
{
 
99
        TypeChannel type_;
 
100
        if("mms"==type)
 
101
                type_ = MMS_CHANNEL;
 
102
        else if("pplive" == type)
 
103
        {
 
104
                if(!parent->support_pplive())
 
105
                        return;
 
106
                else
 
107
                        type_ = PPLIVE_CHANNEL;
 
108
        }
 
109
        else if("sopcast" == type)
 
110
        {
 
111
                if(!parent->support_sopcast())
 
112
                        return;
 
113
                else
 
114
                        type_ = SOPCAST_CHANNEL;
 
115
        }
 
116
        else if("ppstream" == type)
 
117
        {
 
118
                type_ = PPS_CHANNEL;
 
119
 
 
120
        }
 
121
        else
 
122
                type_ = NONE;
 
123
 
 
124
        Gtk::TreeModel::iterator iter = m_liststore->prepend();
 
125
        (*iter)[columns.name] = name;
 
126
        (*iter)[columns.freq] = 100;
 
127
        (*iter)[columns.stream]=stream_;
 
128
 
 
129
 
 
130
        (*iter)[columns.type]=type_ ;
 
131
 
 
132
}
 
133
 
 
134
void RecentChannel::saveLine(const Glib::ustring & name,const std::string& stream_,TypeChannel type)
 
135
{
 
136
 
 
137
        char buf[512];
 
138
        std::string homedir=Glib::get_user_config_dir();
 
139
        snprintf(buf, 512,"%s/gmlive/recent.lst",homedir.c_str());
 
140
        std::ifstream file(buf);
 
141
        std::vector<std::string> list;
 
142
        if(!file){
 
143
                printf("buf is %s\n",buf);
 
144
                std::cout<<"file error\n";
 
145
                return;
 
146
        }
 
147
        std::string line;
 
148
        int num=0;
 
149
        if(file){
 
150
                while(std::getline(file,line)){
 
151
                        list.push_back(line);
 
152
                        num++;
 
153
                        if(25==num)
 
154
                                break;
 
155
                }
 
156
        }
 
157
        file.close();
 
158
        std::string stream;
 
159
        
 
160
        std::string strtype;
 
161
        if(type == MMS_CHANNEL)
 
162
        {
 
163
                //stream = name +"\t#"+stream_+"\t#mms";
 
164
                strtype = "mms";
 
165
        }
 
166
        else if (type == SOPCAST_CHANNEL)
 
167
        {
 
168
                //stream = name +"\t#"+stream_+"\t#sopcast";
 
169
                strtype = "sopcast";
 
170
        }
 
171
        else if(type == PPLIVE_CHANNEL)
 
172
        {
 
173
                //stream = name +"\t#"+stream_+"\t#pplive";
 
174
                strtype = "pplive";
 
175
        }
 
176
        else if(type ==PPS_CHANNEL)
 
177
        {
 
178
                //stream = name +"\t#"+stream_+"\t#ppstream";
 
179
                strtype = "ppstream";
 
180
 
 
181
        }
 
182
        stream = name +"\t#"+stream_+"\t#"+strtype;
 
183
        std::vector<std::string>::iterator iter = std::find(list.begin(),list.end(),stream);
 
184
        if(iter == list.end())
 
185
        {
 
186
                list.push_back(stream);
 
187
                std::ofstream outfile(buf);
 
188
                for(iter=list.begin();iter!=list.end();++iter)
 
189
                {
 
190
                        if(iter == list.begin()&&(num==10))
 
191
                                ;
 
192
                        else
 
193
                                outfile<<*iter<<std::endl;
 
194
                }
 
195
                outfile.close();
 
196
 
 
197
                int users  =0;
 
198
                addLine(users, name,stream_,strtype);
 
199
        }
 
200
 
 
201
}
 
202