Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
arjide committed Nov 9, 2014
1 parent bf20aa0 commit 197ce54
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 25 deletions.
2 changes: 2 additions & 0 deletions images.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,7 @@
<file>images/blueStar.png</file>
<file>images/login-panel2.svg</file>
<file>images/loading.png</file>
<file>images/dud.png</file>
<file>images/duz.png</file>
</qresource>
</RCC>
Binary file added images/dud.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/duz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 4 additions & 3 deletions qml/Api/QQApi.qml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import QtQuick 2.2
import utility 1.0
import QtQuick.Window 2.1
import QQItemInfo 1.0
import qqstars 1.0

QQ{
id: root
Expand Down Expand Up @@ -249,12 +250,12 @@ QQ{
}

function getAvatarFinished( error, path, name ){//获得自己头像完成
if(error){//如果出错
if(error==DownloadImage.DownloadError){//如果是下载出错
downloadImage(QQItemInfo.Friend, url, myqq.userQQ, "240", getAvatarFinished)//重新获取头像
return
}

myqq.avatar240 = path+"/"+name
if(error==DownloadImage.NoError)
myqq.avatar240 = path+"/"+name
}

function getFriendInfo( tuin,backFun ) {//获取好友资料
Expand Down
2 changes: 1 addition & 1 deletion qml/Chat/DiscuChatPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ ChatPage{
MouseArea{
anchors.fill: parent
onDoubleClicked: {
myqq.addChatPage(item_root.myinfo.uin, QQ.Friend)//双击开始聊天
myqq.addChatPage(item_root.myinfo.uin, QQItemInfo.Friend)//双击开始聊天
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion qml/Chat/GroupChatPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ ChatPage{
MouseArea{
anchors.fill: parent
onDoubleClicked: {
myqq.addChatPage(item_root.myinfo.uin, QQ.Friend)//双击开始聊天
myqq.addChatPage(item_root.myinfo.uin, QQItemInfo.Friend)//双击开始聊天
}
}
}
Expand Down
1 change: 1 addition & 0 deletions qml/Login/LoginPanel/LoginPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
import QtQuick.Particles 2.0
import mywindow 1.0
import qqstars 1.0
import "../../Utility"
import "../"

Expand Down
1 change: 1 addition & 0 deletions qml/MainPanel/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import QtQuick.Window 2.1
import utility 1.0
import "../Utility"
import "../Chat"
import qqstars 1.0
//import Qt.labs.settings 1.0

MyWindow{
Expand Down
1 change: 1 addition & 0 deletions qml/Utility/CodeInput.qml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
import utility 1.0
import qqstars 1.0

MyWindow{
id: root_window
Expand Down
1 change: 1 addition & 0 deletions qml/Utility/SystemTray.qml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import QtQuick 2.2
import mywindow 1.0
import utility 1.0
import QQItemInfo 1.0
import qqstars 1.0

MySystemTrayIcon{
id: root
Expand Down
4 changes: 3 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QQmlContext>
#include <QQmlApplicationEngine>
#include <QScreen>
#include <QFileDialog>
#include "mynetworkaccessmanagerfactory.h"
#include "systemtrayicon.h"
#include "utility.h"
Expand Down Expand Up @@ -42,7 +43,8 @@ int main(int argc, char *argv[])
qmlRegisterType<MenuSeparator>("mywindow", 1,0, "MenuSeparator");
qmlRegisterType<MyMenuItem>("mywindow", 1,0, "MyMenuItem");
qmlRegisterType<MyShortcut>("utility", 1,0, "MyShortcut");
qmlRegisterType<QQCommand>("utility", 1,0, "QQ");
qmlRegisterType<DownloadImage>("utility", 1, 0, "DownloadImage");
qmlRegisterType<QQCommand>("qqstars", 1,0, "QQ");
qmlRegisterType<FriendInfo>("QQItemInfo", 1,0, "FriendInfo");
qmlRegisterType<GroupInfo>("QQItemInfo", 1,0, "GroupInfo");
qmlRegisterType<DiscuInfo>("QQItemInfo", 1,0, "DiscuInfo");
Expand Down
18 changes: 12 additions & 6 deletions src/qqstars/qqstars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ void QQCommand::onNetworkOnlineStateChanged(bool isOnline)
}
}

void QQCommand::downImageFinished(bool error, const QString &path, const QString &name)
void QQCommand::downImageFinished(DownloadImage::ErrorType error, const QString &path, const QString &name)
{
QStringList list = name.split ("_");//分割字符串
QString rootUin=list[0];//图片发送者本身或者图片发送者所在群的uin
Expand All @@ -276,7 +276,7 @@ void QQCommand::downImageFinished(bool error, const QString &path, const QString
}
QQItemInfo* info = createQQItemInfo (rootUin, senderType);
//获取图片所有者的信息
if(error){//如果出错
if(error==DownloadImage::DownloadError){//如果是下载出错
qDebug()<<"QQCommand:图片"<<name<<"下载出错";
QString image_url = getImageUrlById (imageID);//通过图片id获得储存的图片真实下载地址
QString save_name = name.mid (0, name.size ()-4);//去除末尾的图片后缀才是要保存的图片名字
Expand All @@ -285,11 +285,17 @@ void QQCommand::downImageFinished(bool error, const QString &path, const QString
QUrl(image_url), path, save_name);//重新下载图片
return;
}

ChatMessageInfo* message_info = info->getChatMessageInfoById (messageID);
//通过图片所在消息的id获取储存消息信息的对象
QString content = message_info->contentData ();//获得消息储存的内容
QString old_img = "<img src=\"qrc:/images/loading.png\" imageID="+QString::number (imageID)+">";//旧的img标签中的内容
QString new_img = "<img src=\"file:///"+path+name+"\">";//新img标签内容
QString old_img = "<img src=\"qrc:/images/duz.png\" imageID="+QString::number (imageID)+">";//旧的img标签中的内容
QString new_img;
if(error==DownloadImage::SaveError||error==DownloadImage::NotSupportFormat){
new_img = "<img src=\"qrc:/images/dud.png\">";//新img标签内容
}else if(error==DownloadImage::NoError){//如果没有错误
new_img = "<img src=\"file:///"+path+"/"+name+"\">";//新img标签内容
}
content.replace (old_img, new_img);//将old_img标签替换为新的内容
message_info->setContentData (content);//替换消息内容,qml端会自动刷新消息
}
Expand All @@ -306,7 +312,7 @@ void QQCommand::getImageUrlFinished(QNetworkReply *replys)
}
const QQItemInfo* root_info = image_info.messageInfo->getParent ();
//获取图片所有者信息的对象(如果图片为好友发送,那就是那个好友的info,如果是群,那就是群的ifno)
QString save_path = root_info->localCachePath ()+"/cacheImage/";//获取图片的缓存路径
QString save_path = root_info->localCachePath ()+"/cacheImage";//获取图片的缓存路径
QString save_name = root_info->uin ()+"_"+
QString::number (image_info.messageInfo->messageId ())+"_"+
QString::number (image_info.imageID)+"_"+
Expand Down Expand Up @@ -987,7 +993,7 @@ QString QQCommand::disposeImageMessage(ChatMessageInfo* message_info, QString im
queue_imageInfo<<image_info;//将图片的信息加入队列
http_image->get (this, SLOT(getImageUrlFinished(QNetworkReply*)), image_url);
//进行网络请求,获取图片的真实下载地址
return "<img src=\"qrc:/images/loading.png\" imageID="+QString::number (image_id)+">";
return "<img src=\"qrc:/images/duz.png\" imageID="+QString::number (image_id)+">";
//返回此字符串,用于img标签的占位,等图片下载完成会替换此img标签
}

Expand Down
3 changes: 2 additions & 1 deletion src/qqstars/qqstars.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <QQuickItem>
#include <QJSEngine>
#include "qqiteminfo.h"
#include "downloadimage.h"

class MyWindow;
class MyHttpRequest;
Expand Down Expand Up @@ -83,7 +84,7 @@ private slots:
void onStateChanged();//当状态改变后调用,将状态存到本地
void onPoll2Timeout();//如果心跳包超时
void onNetworkOnlineStateChanged(bool isOnline);//如果网络在线状态改变
void downImageFinished(bool error, const QString& path, const QString& name);
void downImageFinished(DownloadImage::ErrorType error, const QString& path, const QString& name);
//下载图片完成时调用,下载的图片是好友发送过来的
void getImageUrlFinished(QNetworkReply *replys);//获取图片真实的下载地址完成
private:
Expand Down
66 changes: 55 additions & 11 deletions src/utility/downloadimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,85 @@ MyHttpRequest *DownloadImage::getHttpRequest()
return httpRequest;
}

QString DownloadImage::imageFormatToString(const QByteArray &array)
{
QByteArray str = array.toHex ();

if(str.mid (2,6)=="504e47")
return "png";
if(str.mid (12,8)=="4a464946")
return "jpg";
if(str.left (6)=="474946")
return "gif";
if(str.left (4)=="424d")
return "bmp";
return "";
}

void DownloadImage::downloadFinished(QNetworkReply *replys)
{
Data data = queue_data.dequeue ();
ReplyType type=data.replyType;
bool error=false;
ErrorType error=NoError;
QString saveName = data.saveName;
QString savePath = data.savePath;
QString format = ".";

if(replys->error() == QNetworkReply::NoError)
{
QString imageName = savePath;
QDir dir(imageName);
if( !dir.exists () )
dir.mkpath (imageName);
imageName+="/"+saveName+".png";
QByteArray temp=replys->readAll();
QImage image;
image.loadFromData(temp);
if( !image.save (imageName) ){
qDebug()<<"DownloadImage:图片保存失败,收到的网络数据是:"<<temp;
error = true;
imageName+="/"+saveName;
format += imageFormatToString (temp);
if(format!="."){
imageName.append (format);
qDebug()<<"DownloadImage:要保存的图片名为:"<<imageName;
}else{
qDebug()<<"DownloadImage:未知的图片格式"<<temp.toHex ();
error = NotSupportFormat;
return;
}
QFile file(imageName);
if(file.open (QIODevice::WriteOnly)){
file.write (temp);//储存图片
file.close ();
}else{
qDebug()<<"DownloadImage:图片保存失败"<<file.errorString ();
error = SaveError;
}
/*if(format==".gif"){//如果是gif图
QFile file(imageName);
if(file.open (QIODevice::WriteOnly)){
file.write (temp);//储存图片
file.close ();
}else{
qDebug()<<"DownloadImage:图片保存失败"<<file.errorString ();
}
}else{
QImage image;
image.loadFromData(temp);
if( !image.save (imageName) ){
qDebug()<<"DownloadImage:图片保存失败,收到的网络数据是:"<<temp<<endl;
error = true;
}
}*/
}else{
error = true;
error = DownloadError;
}

if(type == CallbackFun){
QJSValueList list;
list<<QJSValue(error)<<QJSValue(savePath)<<QJSValue(saveName+".png");
list<<QJSValue((int)error)<<QJSValue(savePath)<<QJSValue(saveName+format);
data.callbackFun.call (list);
}else if(type == ConnectSlot){
bool ok=QMetaObject::invokeMethod (data.caller, data.slotName,
Q_ARG(bool, error),
Q_ARG(DownloadImage::ErrorType, error),
Q_ARG(QString, savePath),
Q_ARG(QString, saveName+".png"));
Q_ARG(QString, saveName+format));
if(!ok)
qDebug()<<"DownloadImage:调用槽"<<data.slotName<<"出错";
}
Expand Down
16 changes: 15 additions & 1 deletion src/utility/downloadimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@ class QNetworkReply;
class DownloadImage : public QObject
{
Q_OBJECT

Q_ENUMS(ErrorType)
public:
explicit DownloadImage(QObject *parent = 0);
explicit DownloadImage(MyHttpRequest* http, QObject *parent = 0);
MyHttpRequest *getHttpRequest();

static QString imageFormatToString(const QByteArray& array);

enum ErrorType{
NoError,//无错误
DownloadError,//下载出错
SaveError,//保存出错
NotSupportFormat//不支持的图片格式
};

private:
enum ReplyType{//回调的几种方式
CallbackFun,
Expand All @@ -34,11 +44,15 @@ class DownloadImage : public QObject
};
QQueue<Data> queue_data;
MyHttpRequest* httpRequest;

private slots:
void downloadFinished(QNetworkReply *replys);//当图片下载完成的时候调用
public slots:
void getImage(QJSValue callbackFun, QUrl url, QString savePath, QString saveName);
void getImage(QObject *caller, QByteArray slotName, QUrl url, QString savePath, QString saveName);
/*
* savePath中最后一个目录后面不要加"/",saveName不要带后缀
*/
};

#endif // DOWNLOADIMAGE_H

0 comments on commit 197ce54

Please sign in to comment.