shareplugin.cpp

来自「张勇的linQ学习P2P及IM软件的极佳素材代码」· C++ 代码 · 共 211 行

CPP
211
字号
/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   copyright            : (C) 2003 by Zhang Yong                         *
 *   email                : z-yong163@163.com                              *
 ***************************************************************************/

#include "plugin.h"
#include "shareserver.h"
#include "sharewnd.h"
#include "sharewidgetimpl.h"
#include "share.xpm"
#include <qpixmap.h>
#include <qdir.h>


class MyShareServer : public ShareServer {
public:
	MyShareServer(TCPSessionBase *tcp)
		: ShareServer(tcp) {}

private:
	virtual bool getPathName(std::string &result, const char *rel);
	virtual void readDirectory(std::list<ENTRY_INFO> &entries, const char *dir);

	QString buildAbsPath(const char *rel);
};


class ShareSessionFactory : public SessionFactory {
public:
	virtual const char *getType() {
		return "sharefile";
	}
	virtual QString getText() {
		return ShareWnd::tr("Visit &Shared File");
	}
	virtual bool needRequest() { return false; }
	virtual int getAccept(const char *contact);
	virtual const QPixmap *getIcon();
	virtual TCPSessionListener *createSession(TCPSessionBase *tcp);
};


class SharePlugin : public Plugin {
public:
	static SharePlugin *instance();

	virtual bool init(MAPI *mapi);
	virtual void destroy() {
		delete this;
	}
	virtual QString getName() {
		return ShareWnd::tr("Share File");
	}
	virtual QString getDesc() {
		return ShareWnd::tr("Allows you to share files with others!");
	}
	virtual const QPixmap *getIcon() {
		return &iconPix;
	}
	virtual QWidget *getConfigWidget(QWidget *parent);
	virtual void saveConfig(QWidget *w) {
		((ShareWidget *) w)->save(options);
	}

	virtual void load(const QDomElement &pref);
	virtual void unload(QDomDocument &doc, QDomElement &pref);

	ContactList *getContactList() {
		return mapi->getContactList();
	}

	Options options;

private:
	SharePlugin();
	virtual ~SharePlugin();

	MAPI *mapi;
	QPixmap iconPix;
	ShareSessionFactory factory;
};


QString MyShareServer::buildAbsPath(const char *rel)
{
	QString folder = SharePlugin::instance()->options.shareFolder;

	QDir root(folder);
	if (root.isRelative())
		return QString::null;

	QString path = QDir::cleanDirPath(QString::fromUtf8(rel));
	if (!path.startsWith("/"))
		return QString::null;

	path.remove(0, 1);
	return root.filePath(path);
}

bool MyShareServer::getPathName(std::string &result, const char *rel)
{
	result = buildAbsPath(rel).local8Bit();
	return !result.empty();
}

void MyShareServer::readDirectory(std::list<ENTRY_INFO> &entries, const char *rel)
{
	QString path = buildAbsPath(rel);
	if (path.isEmpty())
		return;

	QDir dir(path);
	const QFileInfoList *l = dir.entryInfoList();
	if (!l)
		return;

	QFileInfoListIterator it(*l);
	QFileInfo *fi;

	while ((fi = it.current()) != 0) {
		ENTRY_INFO info;
		info.name = fi->fileName().utf8();
		info.isDir = fi->isDir();
		info.size = fi->size();

		entries.push_back(info);
		++it;
	}
}


const QPixmap *ShareSessionFactory::getIcon()
{
	return SharePlugin::instance()->getIcon();
}

int ShareSessionFactory::getAccept(const char *contact)
{
	Options &ops = SharePlugin::instance()->options;
	return ops.getAccept(SharePlugin::instance()->getContactList(), contact);
}


TCPSessionListener *ShareSessionFactory::createSession(TCPSessionBase *tcp)
{
	if (!tcp->isSender())
		return new MyShareServer(tcp);

	ShareWnd *w = new ShareWnd(tcp);
	w->setIcon(*getIcon());
	w->show();
	return w;
}


SharePlugin *SharePlugin::instance()
{
	static SharePlugin *p = NULL;
	if (!p)
		p = new SharePlugin;
	return p;
}


SharePlugin::SharePlugin()
{
	iconPix = QPixmap((const char **) share_xpm);
}

SharePlugin::~SharePlugin()
{
}

bool SharePlugin::init(MAPI *mapi)
{
	this->mapi = mapi;
	return true;
}

void SharePlugin::load(const QDomElement &pref)
{
	options.load(pref);

	mapi->registerSession(&factory);
}

void SharePlugin::unload(QDomDocument &doc, QDomElement &pref)
{
	options.save(doc, pref);
	mapi->unregisterSession(&factory);
}

QWidget *SharePlugin::getConfigWidget(QWidget *parent)
{
	ShareWidget *w = new ShareWidget(mapi->getContactList(), parent);
	w->load(options);
	return w;
}


LINQ_EXPORT Plugin *getPlugin()
{
	return SharePlugin::instance();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?