summaryrefslogtreecommitdiff
path: root/noncore/applets/keyhelper/keyhelperapplet/anylnk/AnyLnk.cpp
blob: 3c2298ecd5008b8a7df11ad5f674c6b4002c99e6 (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
#include "AnyLnk.h"
#include "KHUtil.h"

void AnyLnk::loadPixmap()
{
	if(m_params.count() >= 3){
		QString& str = m_params[2];
		QImage image = Resource::loadImage(str);
		if(image.isNull() == false){
			const QSize& size = AppLnkManager::getIconSize();
			m_pixmap.convertFromImage(
				image.smoothScale(size.width(), size.height()) );
		}	
	}
}

void AnyLnk::parseText()
{
	if(m_params.count() >= 2){
		QString& str = m_params[1];
		if(str != QString::null && str.length() > 0){
			replaceKeyword(str);
			replaceDate(str);
		}
	}
}

void AnyLnk::replaceText(QString& str, const QString& s1, const QString& s2)
{
	int index = 0;
	int idx;
	int len = s1.length();
	idx = str.find(s1, index);
	for(;;){
		idx = str.find(s1, index);
		if(idx < 0) break;
		str.replace(idx, len, s2);
		index = idx;
	}
}

void AnyLnk::replaceDate(QString& str)
{
	time_t t;
	struct tm lct;
	char buf[4096];
	int nLen;
	QString group;

	t = ::time(NULL);
	::localtime_r(&t, &lct);

	ConfigEx& cfg = ConfigEx::getInstance("keyhelper");
	group = cfg.getGroup();
	cfg.setGroup("Global");
	QString charset = cfg.readEntry("SystemCharSet", "eucJP");
	if(charset.length() == 0){
		charset = "eucJP";
	}
	cfg.setGroup(group);

	QTextCodec* codec = QTextCodec::codecForName(charset);
	if(codec == NULL){
		codec = QTextCodec::codecForLocale();
	}
	QTextDecoder* decoder = codec->makeDecoder();
	QTextEncoder* encoder = codec->makeEncoder();
	nLen = str.length();
	QCString localeString = encoder->fromUnicode(str, nLen);

	memset(buf, '\0', sizeof(buf));
	int n = ::strftime(buf, sizeof(buf), localeString, &lct);
	if(n > 0){
		str = decoder->toUnicode(buf, n);
	}
	delete decoder;
	delete encoder;
}

void AnyLnk::replaceKeyword(QString& str)
{
	QString txt;
	/* clipboard text */
	QClipboard* cb = QApplication::clipboard();
	if(cb == NULL){
		txt == "";
	} else {
		txt = cb->text();
	}
	replaceText(str, "%clipboard%", txt);
	/* current app */
	txt = KHUtil::currentApp();
	replaceText(str, "%currentapp%", txt);
}