您的位置:首页 > 其它

eva0.4.1源码看看6

2013-10-31 00:01 155 查看
/***************************************************************************
*   Copyright (C) 2004 by yunfan                                          *
*   yunfan_zg@163.com                                                     *
*                                                                         *
*   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.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#include "evaconnecter.h"
#include "evahtmlparser.h"
#include "evanetwork.h"
#include "evapacketmanager.h"
#include "evaresource.h"
#include "evasetting.h"
#include "evasocket.h"
#include "evauser.h"
#include "evausersetting.h"
#include "evaservers.h"
#include "qmdcodec.h"
#include "evaqtutil.h"
#include "evapicmanager.h"
#include "evauhmanager.h"
#include "regiongrabber.h"
#include "filetrans/evafilemanager.h"
封了这么多类,c++,就是包来报去,一个文件里面,没啥具体的实现,包了底层,加了几个函数,浪费这么多代码行。
/***************************************************************************
*   Copyright (C) 2004 by yunfan                                          *
*   yunfan_zg@163.com                                                     *
*                                                                         *
*   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.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#ifndef EVARESOURCE_H
#define EVARESOURCE_H
#include <qobject.h>
#include <qfile.h>
#include <qmap.h>
#include <qpixmap.h>
#include <qimage.h>
#include <qmovie.h>  //这个movie播放gif的。。。
#include <qsize.h>
#ifndef MaxFaceNumber  //最大  脸,头像? 数目
#define MaxFaceNumber 101
#endif
class EvaSetting;
class EvaServers;
class QHttp;  //这里又来个http
class EvaImageResource : public QObject
{
Q_OBJECT
public:
EvaImageResource();
~EvaImageResource();

const QString getFacePath() const;
const QString getIconPath() ;
const QString getSmileyPath() const;
const QString getQQShowPath() const;

const int getFaceID(const int fileIndex) const ;
const int getFaceFileIndex(const int faceId);
QPixmap *getFace(const int fileIndex, const bool on = true);
QPixmap *getIcon(QString name);
const QString getIconFullPath(QString name);
const QMovie *getSmiley(const int fileIndex);  //gif的动画

const QMovie *getLoginMovie();

const bool loadImage();
const QString &getImageRootPath() const { return imageRoot; }
void setImageRootPath( QString &path) { imageRoot = path; }

const QString getThemePath() const { return themePath; }
void setThemePath( const QString &path) { themePath = path; }
QPixmap *getQQShow(const int id);
void setUserHeadImage(QMap<int, QImage> imageOnList, QMap<int, QImage> imageOffList);
void addUserHeadImage(const int id, QImage imgOn, QImage imgOff);
QPixmap *getUserHeadPixmap(const int id, bool isGrayscale = false);
signals:
void qqShowReady(const int);
public slots:
void requestQQShow(const int id);
private:
bool loadFace();
bool loadIcon();
bool loadSmiley();
QMap<QString, QPixmap> faceList;
QMap<QString, QPixmap> iconList;
QMap<QString, QString> iconFileNameMap;
QMap<QString, QMovie> smileyList;
QMap<int, QPixmap> imgOnList;
QMap<int, QPixmap> imgOffList;
QMovie loginMng;
int faceId[MaxFaceNumber];  // store faceID, index is the fileID
QString imageRoot;
QString themePath;

bool isDownloadingQQShow;
int downloadID;
QFile qqshowFile;
QString qqshowFilename;
QHttp *http;

private slots:
void slotQQShowDone(bool error);
};
//又是声音,又是图像,不错。
class EvaSoundResource
{
public:
EvaSoundResource();
~EvaSoundResource(){}
//const bool loadSound(){ return false; }
void playNewMessage();
void playSysMessage();
void playOnlineSound();
void setSoundDir(const QString &path) { soundRoot = path; }
const QString &getSoundDir() const { return soundRoot; }
private:
void playSound(const QString &filename);
QString soundRoot;
};
//把所有的资源,初始化
class EvaGlobal
{
public:
EvaGlobal();
virtual ~EvaGlobal();

static QString &getDirPath();
const bool loadImage();
//const bool loadSound();
const bool loadEvaSetting();

EvaImageResource *getImageResource() { return imgResource;}
EvaSoundResource *getSoundResource() { return sndResource;}
EvaSetting *getEvaSetting() { return system; }
EvaServers *getEvaServers() { return servers; }

const QSize &getFaceSize() const { return faceSize; }
void setFaceSize( const QSize size) { faceSize = size; }
private:
void initialize();
void initImage();
void initSound();
void initEvaSetting();
void initServers();

static QString dirPath;
EvaSetting *system;
EvaServers *servers;
EvaImageResource *imgResource;
EvaSoundResource *sndResource;
QSize faceSize;
};
#endif


/***************************************************************************
* Copyright (C) 2004 by yunfan *
* yunfan_zg@163.com *
* *
* 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., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "evaresource.h"
#include "evasetting.h"
#include "evaservers.h"

#include <stdlib.h>
#include <qapplication.h>
#include <qhttp.h>
#include <qdir.h>
#include <qfileinfo.h>
#include <qdatastream.h>
#include <kaudioplayer.h> //哦。这里又用的谁的库?k开头kDE?
#include <kglobal.h>
#include <kstandarddirs.h>

EvaImageResource::EvaImageResource()
{
//imageRoot = qApp->applicationDirPath() + "/images";
themePath = "";
imageRoot = EvaGlobal::getDirPath() + "/image";
isDownloadingQQShow = false;
http = new QHttp();
connect ( http, SIGNAL(done(bool)), SLOT(slotQQShowDone(bool))); //done什么了?
}

EvaImageResource::~EvaImageResource()
{
}

const QString EvaImageResource::getFacePath() const
{
return imageRoot + "/face";
}

const QString EvaImageResource::getIconPath()
{
if(themePath == "" )
themePath = imageRoot + "/theme";
return themePath;
}

const QString EvaImageResource::getSmileyPath() const
{
return imageRoot + "/smiley";
}

const int EvaImageResource::getFaceID(const int fileIndex) const
{
if(fileIndex<=0 || fileIndex> MaxFaceNumber)
return faceId[0];
return faceId[fileIndex-1];
}

const int EvaImageResource::getFaceFileIndex(const int faceId)
{
int index = (faceId<0)?1:faceId/3 + 1;
if(index<0 || index>101) index = 101;
return index;
}

QPixmap *EvaImageResource::getFace(const int fileIndex, const bool on)
{
QString key = QString::number((fileIndex>0)?fileIndex:1);
if(!on) key+="off";
QMap<QString, QPixmap>::Iterator iter = faceList.find(key);
if(iter== faceList.end()) return NULL;
return &(iter.data());
}

QPixmap *EvaImageResource::getIcon(QString name)
{
QMap<QString, QPixmap>::Iterator iter = iconList.find(name);
if(iter== iconList.end()) return NULL;
return &(iter.data());
}

const QString EvaImageResource::getIconFullPath(QString name)
{
QMap<QString, QString>::Iterator iter = iconFileNameMap.find(name);
if(iter== iconFileNameMap.end()) return "";
return getIconPath() + "/" + iter.data();
}

const QMovie *EvaImageResource::getSmiley(const int fileIndex)
{
QString key = QString::number(fileIndex);
QMap<QString, QMovie>::Iterator iter = smileyList.find(key);
if(iter== smileyList.end()) return NULL;
return &(iter.data());
}

const QMovie *EvaImageResource::getLoginMovie()
{
return &loginMng;
}

const bool EvaImageResource::loadImage()
{
return (loadFace() && loadIcon() && loadSmiley());
}

bool EvaImageResource::loadFace()
{
QString path = getFacePath();
QFile file( path + "/face.theme");
if(!file.open(IO_ReadOnly)){
return false;
}

QTextStream stream(&file);
QString line, imagePath;
QStringList lineList;
while(!stream.atEnd()){
line = stream.readLine().stripWhiteSpace();

lineList = QStringList::split(":", line);
if(lineList.size() != 2)
continue;

lineList[0].stripWhiteSpace();
imagePath = path + "/" + lineList[1].stripWhiteSpace();
QPixmap img(imagePath);
if(!img.isNull())
faceList[lineList[0]] = img;
}

file.close();

for(int i = 0; i < MaxFaceNumber ; i++) {
faceId[i] = i * 3 + 1;
}
return true;
}

bool EvaImageResource::loadIcon()
{
QString path = getIconPath() + "/eva.theme";
QDir d;
if(!d.exists(path)){
themePath = ""; // if theme not exist, load default one
path = getIconPath() + "/eva.theme";
}
QFile file( path);
if(!file.open(IO_ReadOnly)){
return false;
}

QTextStream stream(&file);
QString line, imagePath;
QStringList lineList;

while(!stream.atEnd()){
line = stream.readLine().stripWhiteSpace();

lineList = QStringList::split(":", line);

if(lineList.size() != 2)
continue;

lineList[0].stripWhiteSpace();
imagePath = getIconPath() + "/" + lineList[1].stripWhiteSpace();
if(lineList[0] == "LOGIN_MNG"){
loginMng = QMovie(imagePath);
continue;
}
QPixmap img(imagePath);
if(!img.isNull()){
iconList[lineList[0]] = img;
iconFileNameMap[lineList[0]] = lineList[1].stripWhiteSpace();
}
}

file.close();
return true;
}

bool EvaImageResource::loadSmiley()
{
QString path = getSmileyPath();
QFile file( path + "/smiley.theme");
if(!file.open(IO_ReadOnly)){
return false;
}

QTextStream stream(&file);
QString line, imagePath;
QStringList lineList;

while(!stream.atEnd()){
line = stream.readLine().stripWhiteSpace();

lineList = QStringList::split(":", line);

if(lineList.size() != 2)
continue;

lineList[0].stripWhiteSpace();
imagePath = path + "/" + lineList[1].stripWhiteSpace();
QMovie m(imagePath);
if(!m.isNull())
smileyList[lineList[0]] = m;
}

file.close();
return true;
}

const QString EvaImageResource::getQQShowPath() const
{
QString evaHome = QDir::homeDirPath() + "/.eva";//隐藏文件。
QDir d;
if(!d.exists(evaHome)){
return "";
}
if(!d.cd(evaHome)) return "";
if(!d.exists("QQShow")){
if(!d.mkdir("QQShow", false))return "";
}
return evaHome + "/QQShow";
}

QPixmap *EvaImageResource::getQQShow(const int id)
{
QString path = getQQShowPath() +"/"+ QString::number(id) + ".gif";
QDir d;
if(d.exists(path))
return new QPixmap(path);
return NULL;
}

void EvaImageResource::requestQQShow(const int id)
{
if(isDownloadingQQShow) return;
QString path = getQQShowPath();
if(path == "") return;

qqshowFile.setName(path +"/"+ QString::number(id) + ".gif" );
if(qqshowFile.exists()){
qqshowFile.remove();
}

if ( !qqshowFile.open( IO_WriteOnly ) ) {
printf("cannot create the image\n");
return;
}
http->setHost( "qqshow-user.tencent.com" ); //哦,从网站下载下来的。这个网址,连接,永远不变吗?
int picNum = rand()*100;
QString remoteFile = "/"+QString::number(id) + "/10/00/" + QString::number(picNum) +".gif";
downloadID = id;
http->get( remoteFile, &qqshowFile );
}

void EvaImageResource::slotQQShowDone( bool error )
{
qqshowFile.close();
if(error){
printf("download error: %d\n",error);
isDownloadingQQShow = false;
return;
}
isDownloadingQQShow = false;
emit qqShowReady(downloadID);
}

void EvaImageResource::setUserHeadImage(QMap<int, QImage> imageOnList, QMap<int, QImage> imageOffList)
{
QMap<int, QImage>::Iterator it = imageOnList.begin();
while(it != imageOnList.end()){
imgOnList[it.key()] = QPixmap(it.data());
++it;
}
it = imageOffList.begin();
while(it != imageOffList.end()){
imgOffList[it.key()] = QPixmap(it.data());
++it;
}
}

QPixmap *EvaImageResource::getUserHeadPixmap(const int id, bool isGrayscale)
{
QPixmap * result;
QMap<int, QPixmap>::Iterator it;
if(isGrayscale){
it = imgOffList.find(id);
if(it == imgOffList.end())
result = NULL;
else
result = &(it.data());
}else{
it = imgOnList.find(id);
if(it == imgOnList.end()){
result = NULL;
}else
result = &(it.data());
}
return result;
}

void EvaImageResource::addUserHeadImage(const int id, QImage imgOn, QImage imgOff)
{
imgOnList[id] = QPixmap(imgOn);
imgOffList[id] = QPixmap(imgOff);
}

/* ---------------------------------------------------------------------------------------------- */

EvaSoundResource::EvaSoundResource()
{
soundRoot = EvaGlobal::getDirPath() + "/sound";
}

void EvaSoundResource::playNewMessage()
{
playSound("msg.wav");
}

void EvaSoundResource::playSysMessage()
{
playSound("system.wav");
}

void EvaSoundResource::playOnlineSound()
{
playSound("online.wav");
}

void EvaSoundResource::playSound(const QString &filename)
{
QString absPath = soundRoot + "/" + filename;
QDir d;
if(!d.exists(absPath)){
absPath = EvaGlobal::getDirPath() + "/sound" + "/" + filename;
}

if(!d.exists(absPath))return;

KAudioPlayer snd(absPath);
snd.play();
}

/* ---------------------------------------------------------------------------------------------- */

QString EvaGlobal::dirPath = "~/eva";

EvaGlobal::EvaGlobal()
{
initialize();
}

EvaGlobal::~EvaGlobal()
{
delete imgResource;
delete sndResource;
delete system;
delete servers;
}

QString &EvaGlobal::getDirPath()
{
return dirPath;
}

const bool EvaGlobal::loadImage()
{
if(!imgResource) return false;
return imgResource->loadImage();
}
/*
const bool EvaGlobal::loadSound()
{
if(!sndResource) return false;
return sndResource->loadSound();
}*/

const bool EvaGlobal::loadEvaSetting()
{
if(!system) return false;
return system->loadSetting();
}

void EvaGlobal::initialize()
{
dirPath = KGlobal::dirs()->findResource("data", QString::fromLatin1("eva/servers"));
// QStringList dirs = KGlobal::dirs()->findDirs("data", QString::fromLatin1("eva"));
// for(uint i=0; i<dirs.size(); i++){
// //printf("dir %i:%s\n", i, dirs[i].ascii());
// //dirPath = dirs[0];
// break;
// }
if(dirPath == QString::null){
QFileInfo fi;
fi.setFile(QString(getenv("_")));
dirPath = fi.dirPath(true);
}else
dirPath = dirPath.left(dirPath.length() - strlen("/servers"));
//printf("found data path: %s\n", dirPath.ascii());
initImage();
initSound();
initEvaSetting();
initServers();
}

void EvaGlobal::initImage()
{
imgResource = new EvaImageResource();
}

void EvaGlobal::initSound()
{
sndResource = new EvaSoundResource();
}

void EvaGlobal::initEvaSetting()
{
system = new EvaSetting();
}

void EvaGlobal::initServers( )
{
servers = new EvaServers(dirPath);
}

还是只是调用一堆接口的文件。睡觉12:00
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: