summaryrefslogtreecommitdiff
path: root/plugins/Folders/src/folderItem.cpp
blob: f89f0016196cea87ce52fb83c301c2dadd20a94f (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
/*
Custom profile folders plugin for Miranda IM

Copyright © 2005 Cristian Libotean

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 "commonheaders.h"

CFolderItem::CFolderItem(const char *sectionName, const char *name, const TCHAR *format, const TCHAR *userName)
{
	m_szSection = mir_strdup(sectionName);
	m_szName = mir_strdup(name);
	if (userName)
		m_tszUserName = mir_tstrdup(userName);
	else
		m_tszUserName = mir_a2t(name);
	m_tszFormat = NULL;
	m_tszOldFormat = NULL;
	GetDataFromDatabase(format);
	FolderCreateDirectory();
}

CFolderItem::~CFolderItem()
{
	mir_free(m_szSection);
	mir_free(m_szName);
	mir_free(m_tszFormat);
	mir_free(m_tszOldFormat);
	mir_free(m_tszUserName);
}

void CFolderItem::SetFormat(const TCHAR *newFormat)
{
	mir_free(m_tszOldFormat);
	m_tszOldFormat = m_tszFormat;
	m_tszFormat = mir_tstrdup( *newFormat ? newFormat : MIRANDA_PATHT);
}

int CFolderItem::IsEqual(const CFolderItem *other)
{
	return (IsEqual(other->GetSection(), other->GetUserName()));
}

int CFolderItem::IsEqual(const char *section, const TCHAR *name)
{
	return !_tcscmp(m_tszUserName, name) && !strcmp(m_szSection, section);
}

int CFolderItem::IsEqualTranslated(const char *trSection, const TCHAR *trName)
{
	return !_tcscmp( TranslateTS(m_tszUserName), trName) && !strcmp(Translate(m_szSection), trSection);
}

int CFolderItem::operator ==(const CFolderItem *other)
{
	return IsEqual(other);
}

void CFolderItem::Expand(TCHAR *buffer, int size)
{
	ExpandPath(buffer, m_tszFormat, size);
}

void CFolderItem::Save()
{
	FolderDeleteOldDirectory(FALSE);
	FolderCreateDirectory(FALSE);
	WriteDataToDatabase();
}

int CFolderItem::FolderCreateDirectory(int showFolder)
{
	if (m_tszFormat == NULL)
		return FOLDER_SUCCESS;

	TCHAR buffer[MAX_FOLDER_SIZE];
	ExpandPath(buffer, m_tszFormat, SIZEOF(buffer));
	CreateDirectoryTreeT(buffer);
	if (showFolder)
		ShellExecute(NULL, L"explore", buffer, NULL, NULL, SW_SHOW);

	return (DirectoryExists(buffer)) ? FOLDER_SUCCESS : FOLDER_FAILURE;
}

int CFolderItem::FolderDeleteOldDirectory(int showFolder)
{
	if ( !m_tszOldFormat)
		return FOLDER_SUCCESS;

	if ( !_tcscmp(m_tszFormat, m_tszOldFormat)) //format wasn't changed
		return FOLDER_SUCCESS;

	TCHAR buffer[MAX_FOLDER_SIZE];
	ExpandPath(buffer, m_tszOldFormat, SIZEOF(buffer));
	RemoveDirectories(buffer);
	int res = (DirectoryExists(buffer)) ? FOLDER_FAILURE : FOLDER_SUCCESS;
	if ((res == FOLDER_FAILURE) && (showFolder))
		ShellExecute(NULL, _T("explore"), buffer, NULL, NULL, SW_SHOW);
	return res;
}

void CFolderItem::GetDataFromDatabase(const TCHAR *szNotFound)
{
	char name[256];
	strcpy_s(name, sizeof(name), m_szSection);
	strcat_s(name, sizeof(name), m_szName);

	TCHAR buffer[MAX_FOLDER_SIZE];
	GetStringFromDatabase(name, szNotFound, buffer, SIZEOF(buffer));
	SetFormat(buffer);
}

void CFolderItem::WriteDataToDatabase()
{
	char szSettingName[256];
	strcpy_s(szSettingName, sizeof(szSettingName), m_szSection);
	strcat_s(szSettingName, sizeof(szSettingName), m_szName);

	if (m_tszFormat)
		db_set_ts(NULL, ModuleName, szSettingName, m_tszFormat);
}