summaryrefslogtreecommitdiff
path: root/protocols/GTalkExt/src/tipper_items.cpp
blob: e2420305b9fd7865e5719fa8e7a8e3aa9d7698ac (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//***************************************************************************************
//
//   Google Extension plugin for the Miranda IM's Jabber protocol
//   Copyright (c) 2011 bems@jabber.org, George Hazan (ghazan@jabber.ru)
//
//   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 "StdAfx.h"
#include "resources.h"

#define VALUE_SETTING_PROP "DIValue%d"
#define LABEL_SETTING_PROP "DILabel%d"

#define LAST_WRITTEN_LABEL_SETTING "LastWrittenTipperLabel"

static LPSTR TipperItemProps[] =
{
	LABEL_SETTING_PROP,
	"DILineAbove%d",
	"DITipperVarsFirst%d",
	"DIValNewline%d",
	VALUE_SETTING_PROP,
	"DIVisible%d"
};

#define TIPPER_ITEMS_MOD_NAME     "Tipper_Items"
#define TIPPER_ITEM_COUNT_SETTING "DINumValues"
#define UNREAD_THREADS_RAW        _T("%raw:") _T(SHORT_PLUGIN_NAME) _T("/UnreadThreads%")
#define UNREAD_THREADS_LABEL      LPGENT("Unread threads:")

void ShiftTipperSettings(LPSTR buff, int count, LPSTR format)
{
	for (int i = count; i > 0; i--) {
		DBVARIANT dbv;
		sprintf(buff, format, i - 1); //!!!!!!!!!!!!!!!

		if (db_get(0, TIPPER_ITEMS_MOD_NAME, buff, &dbv))
			break;

		__try {
			if (DBVT_ASCIIZ == dbv.type) {
				db_free(&dbv);
				if (db_get_ws(0, TIPPER_ITEMS_MOD_NAME, buff, &dbv))
					break;
			}

			if (db_get_s(0, TIPPER_ITEMS_MOD_NAME, buff, &dbv))
				break;

			sprintf(buff, format, i); //!!!!!!!!!!!!!!!!
			db_set(NULL, TIPPER_ITEMS_MOD_NAME, buff, &dbv);
		}
		__finally {
			db_free(&dbv);
		}
	}
}

void SetLabelProp(int index, LPSTR setting)
{
	sprintf(setting, LABEL_SETTING_PROP, index); //!!!!!!!!!!!!!!

	ptrT tszProp( db_get_tsa(0, TIPPER_ITEMS_MOD_NAME, setting));
	if (tszProp == NULL)
		return;

	ptrT tszLastWritten( db_get_tsa(0, SHORT_PLUGIN_NAME, LAST_WRITTEN_LABEL_SETTING));
	if (tszLastWritten == NULL)
		return;

	if ( !mir_tstrcmp(tszProp, tszLastWritten)) {
		LPTSTR label = TranslateTS(UNREAD_THREADS_LABEL);
		db_set_ts(0, SHORT_PLUGIN_NAME, LAST_WRITTEN_LABEL_SETTING, label);
		db_set_ts(0, TIPPER_ITEMS_MOD_NAME, setting, label);
	}
}

void AddTipperItem()
{
	WORD itemCount = db_get_w(0, TIPPER_ITEMS_MOD_NAME, TIPPER_ITEM_COUNT_SETTING, WORD(-1));
	if (itemCount == WORD(-1))
		return;

	int i, l = 0;
	for (i = itemCount; i > 0; i /= 10) l++; // var setting path
	l += 30; // const setting part

	LPSTR setting = (LPSTR)_alloca(l * sizeof(TCHAR));

	for (i = 0; i < itemCount; i++) {
		mir_snprintf(setting, l, VALUE_SETTING_PROP, i);
		ptrT tszSetting( db_get_tsa(0, TIPPER_ITEMS_MOD_NAME, setting));
		if (tszSetting) {
			if ( !mir_tstrcmp(UNREAD_THREADS_RAW, tszSetting)) {
				SetLabelProp(i, setting);
				return;
			}
		}
	}

	for (i = 0; i < SIZEOF(TipperItemProps); i++)
		ShiftTipperSettings(setting, itemCount, TipperItemProps[i]);

	#define WRITE_TIPPER_PROPS(index, value)\
		mir_snprintf(setting, l, TipperItemProps[##index##], 0);\
		db_set_ts(0, TIPPER_ITEMS_MOD_NAME, setting, ##value##)
	#define WRITE_TIPPER_PROPB(index, value)\
		mir_snprintf(setting, l, TipperItemProps[##index##], 0);\
		db_set_b(0, TIPPER_ITEMS_MOD_NAME, setting, ##value##)

	LPTSTR label = TranslateTS(UNREAD_THREADS_LABEL);

	db_set_ts(0, SHORT_PLUGIN_NAME, LAST_WRITTEN_LABEL_SETTING, label);

	WRITE_TIPPER_PROPS(0, label);
	WRITE_TIPPER_PROPB(1, 0);
	WRITE_TIPPER_PROPB(2, 0);
	WRITE_TIPPER_PROPB(3, 0);
	WRITE_TIPPER_PROPS(4, UNREAD_THREADS_RAW);
	WRITE_TIPPER_PROPB(5, 1);

	db_set_w(0, TIPPER_ITEMS_MOD_NAME, TIPPER_ITEM_COUNT_SETTING, itemCount+1);
}