summaryrefslogtreecommitdiffstats
path: root/src/filemetainfo.cpp
blob: 21dd9495f8e3ee6f312ddf7d7b983a7c02e1ee39 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/***************************************************************************
 *   Copyright (C) 2005 by David Saxton                                    *
 *   david@bluehaze.org                                                    *
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/

#include "filemetainfo.h"
#include "textdocument.h"
#include "textview.h"

#include <kconfig.h>


//BEGIN class MetaInfo
MetaInfo::MetaInfo()
{
	m_cursorLine = 0;
	m_cursorColumn = 0;
}


bool MetaInfo::hasDefaultData() const
{
	return bookmarks().isEmpty() &&
			breakpoints().isEmpty()  &&
			(m_outputMethodInfo.method() == OutputMethodInfo::Method::Direct ) &&
			(m_cursorLine == 0) &&
			(m_cursorColumn == 0);
}


void MetaInfo::save( TDEConfig * conf )
{
	conf->writeEntry( "Bookmarks", bookmarks() );
	conf->writeEntry( "Breakpoints", breakpoints() );
	conf->writeEntry( "OutputMethod", toID(outputMethodInfo().method()) );
	conf->writePathEntry( "OutputPath", outputMethodInfo().outputFile().prettyURL() );
	conf->writeEntry( "OutputPicID", outputMethodInfo().picID() );
	conf->writeEntry( "CursorLine", cursorLine() );
	conf->writeEntry( "CursorColumn", cursorColumn() );
}


void MetaInfo::load( TDEConfig * conf )
{
	setBookmarks( conf->readIntListEntry("Bookmarks") );
	setBreakpoints( conf->readIntListEntry("Breakpoints") );
	m_outputMethodInfo.setMethod( toMethod( conf->readEntry("OutputMethod") ) );
	m_outputMethodInfo.setOutputFile( conf->readPathEntry("OutputPath") );
	m_outputMethodInfo.setPicID( conf->readEntry("OutputPicID") );
	setCursorLine( conf->readNumEntry( "CursorLine", 0 ) );
	setCursorColumn( conf->readNumEntry( "CursorColumn", 0 ) );
}


OutputMethodInfo::Method::Type MetaInfo::toMethod( const TQString & id )
{
	if ( id == "SaveAndLoad" )
		return OutputMethodInfo::Method::SaveAndLoad;
	
	else if ( id == "SaveAndForget" )
		return OutputMethodInfo::Method::SaveAndForget;
	
	return OutputMethodInfo::Method::Direct;
}


TQString MetaInfo::toID( OutputMethodInfo::Method::Type method )
{
	switch (method)
	{
		case OutputMethodInfo::Method::SaveAndLoad:
			return "SaveAndLoad";
			
		case OutputMethodInfo::Method::SaveAndForget:
			return "SaveAndForget";
			
		case OutputMethodInfo::Method::Direct:
		default:
			return "Direct";
	}
}
//END class MetaInfo


//BEGIN class FileMetaInfo
FileMetaInfo::FileMetaInfo()
	: TQObject()
{
	m_metaInfoConfig = new TDEConfig( "metainfo", false, false, "appdata" );
	loadAllMetaInfo();
}


FileMetaInfo::~FileMetaInfo()
{
	saveAllMetaInfo();
	delete m_metaInfoConfig;
}


void FileMetaInfo::grabMetaInfo( const KURL & url, TextDocument * textDocument )
{
	if (!textDocument)
		return;
	
	m_metaInfoMap[url].setBookmarks( textDocument->bookmarkList() );
	m_metaInfoMap[url].setBreakpoints( textDocument->breakpointList() );
}


void FileMetaInfo::initializeFromMetaInfo( const KURL & url, TextDocument * textDocument )
{
	if (!textDocument)
		return;
	
	textDocument->setBookmarks(m_metaInfoMap[url].bookmarks());
	textDocument->setBreakpoints(m_metaInfoMap[url].breakpoints());
}


void FileMetaInfo::grabMetaInfo( const KURL & url, TextView * textView )
{
	if (!textView)
		return;
	
	m_metaInfoMap[url].setCursorLine( textView->currentLine() );
	m_metaInfoMap[url].setCursorColumn( textView->currentColumn() );
}


void FileMetaInfo::initializeFromMetaInfo( const KURL & url, TextView * textView )
{
	if (!textView)
		return;
	
	textView->setCursorPosition( m_metaInfoMap[url].cursorLine(), m_metaInfoMap[url].cursorColumn() );
}


void FileMetaInfo::grabMetaInfo( const KURL & url, OutputMethodDlg * dlg )
{
	if (!dlg)
		return;
	
	m_metaInfoMap[url].setOutputMethodInfo( dlg->info() );
}


void FileMetaInfo::initializeFromMetaInfo( const KURL & url, OutputMethodDlg * dlg )
{
	if ( !dlg || url.isEmpty() || !m_metaInfoMap.contains(url) )
		return;
	
	OutputMethodInfo::Method::Type method = m_metaInfoMap[url].outputMethodInfo().method();
	dlg->setMethod(method);
	
	if ( method != OutputMethodInfo::Method::Direct )
		dlg->setOutputFile( m_metaInfoMap[url].outputMethodInfo().outputFile() );
	
	dlg->setPicID( m_metaInfoMap[url].outputMethodInfo().picID() );
}


void FileMetaInfo::saveAllMetaInfo()
{
	const MetaInfoMap::iterator end = m_metaInfoMap.end();
	for ( MetaInfoMap::iterator it = m_metaInfoMap.begin(); it != end; ++it )
	{
		if ( it.data().hasDefaultData() )
			m_metaInfoConfig->deleteGroup(it.key().prettyURL());
		
		else
		{
			m_metaInfoConfig->setGroup( it.key().prettyURL() );
			it.data().save( m_metaInfoConfig );
		}
	}
}


void FileMetaInfo::loadAllMetaInfo()
{
	TQStringList urlList = m_metaInfoConfig->groupList();
	const TQStringList::iterator end = urlList.end();
	for ( TQStringList::iterator it = urlList.begin(); it != end; ++it )
	{
		m_metaInfoConfig->setGroup(*it);
		m_metaInfoMap[*it].load(m_metaInfoConfig);
	}
}
//END class FileMetaInfo

#include "filemetainfo.moc"