1 //------------------------------------------------------------------------------
2 // emSvgFileModel.cpp
3 //
4 // Copyright (C) 2010,2014,2018 Oliver Hamann.
5 //
6 // Homepage: http://eaglemode.sourceforge.net/
7 //
8 // This program is free software: you can redistribute it and/or modify it under
9 // the terms of the GNU General Public License version 3 as published by the
10 // Free Software Foundation.
11 //
12 // This program is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU General Public License version 3 for
15 // more details.
16 //
17 // You should have received a copy of the GNU General Public License version 3
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 //------------------------------------------------------------------------------
20 
21 #include <emSvg/emSvgFileModel.h>
22 
23 
Acquire(emContext & context,const emString & name,bool common)24 emRef<emSvgFileModel> emSvgFileModel::Acquire(
25 	emContext & context, const emString & name, bool common
26 )
27 {
28 	EM_IMPL_ACQUIRE(emSvgFileModel,context,name,common)
29 }
30 
31 
emSvgFileModel(emContext & context,const emString & name)32 emSvgFileModel::emSvgFileModel(emContext & context, const emString & name)
33 	: emFileModel(context,name)
34 {
35 	ServerModel=emSvgServerModel::Acquire(GetRootContext());
36 	JobHandle=NULL;
37 	SvgHandle=NULL;
38 	FileSize=0;
39 	Width=0.0;
40 	Height=0.0;
41 }
42 
43 
~emSvgFileModel()44 emSvgFileModel::~emSvgFileModel()
45 {
46 	emSvgFileModel::QuitLoading();
47 	emSvgFileModel::QuitSaving();
48 	emSvgFileModel::ResetData();
49 }
50 
51 
ResetData()52 void emSvgFileModel::ResetData()
53 {
54 	if (SvgHandle) {
55 		ServerModel->CloseSvg(SvgHandle);
56 		SvgHandle=NULL;
57 	}
58 	FileSize=0;
59 	Width=0.0;
60 	Height=0.0;
61 	Title.Clear();
62 	Description.Clear();
63 }
64 
65 
TryStartLoading()66 void emSvgFileModel::TryStartLoading()
67 {
68 	FileSize=emTryGetFileSize(GetFilePath());
69 }
70 
71 
TryContinueLoading()72 bool emSvgFileModel::TryContinueLoading()
73 {
74 	if (!JobHandle) {
75 		JobHandle=ServerModel->StartOpenJob(GetFilePath(),&SvgHandle);
76 		return false;
77 	}
78 	ServerModel->Poll(10);
79 	switch (ServerModel->GetJobState(JobHandle)) {
80 	case emSvgServerModel::JS_ERROR:
81 		throw emException("%s",ServerModel->GetJobErrorText(JobHandle).Get());
82 	case emSvgServerModel::JS_SUCCESS:
83 		Width=ServerModel->GetSvgWidth(SvgHandle);
84 		Height=ServerModel->GetSvgHeight(SvgHandle);
85 		Title=ServerModel->GetSvgTitle(SvgHandle);
86 		Description=ServerModel->GetSvgDescription(SvgHandle);
87 		return true;
88 	default:
89 		break;
90 	}
91 	return false;
92 }
93 
94 
QuitLoading()95 void emSvgFileModel::QuitLoading()
96 {
97 	if (JobHandle) {
98 		ServerModel->CloseJob(JobHandle);
99 		JobHandle=NULL;
100 	}
101 }
102 
103 
TryStartSaving()104 void emSvgFileModel::TryStartSaving()
105 {
106 	throw emException("emSvgFileModel: Saving not supported.");
107 }
108 
109 
TryContinueSaving()110 bool emSvgFileModel::TryContinueSaving()
111 {
112 	return true;
113 }
114 
115 
QuitSaving()116 void emSvgFileModel::QuitSaving()
117 {
118 }
119 
120 
CalcMemoryNeed()121 emUInt64 emSvgFileModel::CalcMemoryNeed()
122 {
123 	return 3000000+10*FileSize;
124 }
125 
126 
CalcFileProgress()127 double emSvgFileModel::CalcFileProgress()
128 {
129 	if (
130 		JobHandle &&
131 		ServerModel->GetJobState(JobHandle)!=emSvgServerModel::JS_WAITING
132 	) return 50.0;
133 	return 0.0;
134 }
135