1 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html lang=
"et" xmlns=
"http://www.w3.org/1999/xhtml" xml:
lang=
"et">
5 <meta http-equiv=
"CONTENT-TYPE" content=
"text/html; charset=utf-8" />
6 <title>eVaf Tutorial -
04 - Generator Module
</title>
7 <meta name=
"Author" content=
"Enar Väikene" />
8 <meta name=
"description" content=
"eVaf Tutorial" />
9 <meta name=
"keywords" content=
"evaf c++ application development framework tutorial password generator" />
10 <link rel=
"StyleSheet" href=
"evaf.css" type=
"text/css" media=
"all" />
11 <link rel=
"StyleSheet" href=
"highlight.css" type=
"text/css" media=
"all" />
16 <p>Next:
<a href=
"pswgen05.html">05 - Building Generator Module
</a>, Previous:
<a href=
"pswgen03.html">03 - Generator Module
</a></p>
18 <h1>eVaf Tutorial
</h1>
20 <h2>04 - Generator Module
</h2>
22 <p>Now we are going to implement all the classes declared in the
<tt>module.h
</tt>. Create the
<tt>module.cpp
</tt> file
23 in the
<tt>src/apps/PswGen/Generator
</tt> directory. We obviously include the
<tt>module.h
</tt> header file, but also
24 the
<tt>QtCore
</tt> header file for any non-GUI Qt classes.
</p>
26 <pre class=
"hl"><span class=
"hl com">/**
</span>
27 <span class=
"hl com"> *
@file src/apps/PswGen/Generator/module.cpp
</span>
28 <span class=
"hl com"> */
</span>
30 <span class=
"hl ppc">#include
</span> <span class=
"hl pps">"module.h
"</span><span class=
"hl ppc"></span>
32 <span class=
"hl ppc">#include
<QtCore
></span></pre>
34 <p>All the eVaf modules need to include version information. This is common for all the modules and we can simply
35 copy existing version info files from another eVaf module:
</p>
37 <pre>evaf/src/apps/PswGen/Generator $
<code>cp ../../../plugins/SdiWindow/version.{h,rc} .
</code></pre>
39 <p>The
<tt>version.h
</tt> file contains version information for the module. The
<tt>version.rc
</tt> is for Windows
40 builds only and embeds the same version information into the dll or exe file. Modify the copied
<tt>version.h
</tt>
41 file for our new module. The
<tt>version.rc
</tt> file uses the values from the
<tt>version.h
</tt> and does not
42 need to be touched.
</p>
44 <pre class=
"hl"><span class=
"hl com">/**
</span>
45 <span class=
"hl com"> *
@file src/apps/PswGen/Generator/version.h
</span>
46 <span class=
"hl com"> */
</span>
47 <span class=
"hl ppc">#ifndef __PSWGEN_GENERATOR_VERSION_H
</span>
48 <span class=
"hl ppc">#define __PSWGEN_GENERATOR_VERSION_H
</span>
50 <span class=
"hl ppc">#include
<version_rc.h
></span>
52 <span class=
"hl com">/**
</span>
53 <span class=
"hl com"> * Module/library version number in the form major,minor,release,build
</span>
54 <span class=
"hl com"> */
</span>
55 <span class=
"hl ppc">#define VER_FILE_VERSION
0,
1,
1,
1</span>
57 <span class=
"hl com">/**
</span>
58 <span class=
"hl com"> * Module/library version number in the string format (shall end with \
0)
</span>
59 <span class=
"hl com"> */
</span>
60 <span class=
"hl ppc">#define VER_FILE_VERSION_STR
</span> <span class=
"hl pps">"0.1.1.1\
0"</span><span class=
"hl ppc"></span>
62 <span class=
"hl com">/**
</span>
63 <span class=
"hl com"> * Module/library name (shall end with \
0)
</span>
64 <span class=
"hl com"> */
</span>
65 <span class=
"hl ppc">#define VER_MODULE_NAME_STR
</span> <span class=
"hl pps">"PswGen\
0"</span><span class=
"hl ppc"></span>
67 <span class=
"hl com">/**
</span>
68 <span class=
"hl com"> * Module type (see version_rc.h for all the types)
</span>
69 <span class=
"hl com"> */
</span>
70 <span class=
"hl ppc">#define VER_MODULE_TYPE MT_GENERIC
</span>
72 <span class=
"hl com">/**
</span>
73 <span class=
"hl com"> * Module type in the string format (see version_rc for all the types)
</span>
74 <span class=
"hl com"> */
</span>
75 <span class=
"hl ppc">#define VER_MODULE_TYPE_STR MT_GENERIC
</span>
77 <span class=
"hl com">/**
</span>
78 <span class=
"hl com"> * Original file name for windows (shall end with \
0)
</span>
79 <span class=
"hl com"> */
</span>
80 <span class=
"hl ppc">#define VER_ORIGINAL_FILE_NAME_STR
</span> <span class=
"hl pps">"PswGen.dll\
0"</span><span class=
"hl ppc"></span>
82 <span class=
"hl com">/**
</span>
83 <span class=
"hl com"> * Description of the module/library (shall end with \
0)
</span>
84 <span class=
"hl com"> */
</span>
85 <span class=
"hl ppc">#define VER_FILE_DESCRIPTION_STR
</span> <span class=
"hl pps">"Module that generates strong passwords using MD5 hashes.\
0"</span><span class=
"hl ppc"></span>
87 <span class=
"hl ppc">#endif
</span> <span class=
"hl slc">// version.h
</span><span class=
"hl ppc"></span></pre>
89 <p>Then include the version info file in the
<tt>module.cpp
</tt> file and use the
<tt>VER_EXPORT_VERSION_INFO()
</tt> macro
90 to export version information from the module. This macro defines a public function that all the eVaf modules export and
91 is used to collect version information from them. In your modules you only need to modify the
<tt>version.h
</tt> file and then
92 use the
<tt>VER_EXPORT_VERSION_INFO()
</tt> macro once somewhere in your code.
</p>
94 <pre class=
"hl"><span class=
"hl ppc">#include
</span> <span class=
"hl pps">"version.h
"</span><span class=
"hl ppc"></span>
96 <span class=
"hl kwd">VER_EXPORT_VERSION_INFO
</span><span class=
"hl opt">()
</span></pre>
98 <p>To make the
<tt>Module
</tt> class a proper Qt plugin, we use the
<tt>Q_EXPORT_PLUGIN2
</tt> macro. The name of the module is already
99 defined in the
<tt>version.h
</tt> header file as
<tt>VER_MODULE_NAME_STR
</tt>.
101 <pre class=
"hl"><span class=
"hl kwd">Q_EXPORT_PLUGIN2
</span><span class=
"hl opt">(
</span>VER_MODULE_NAME_STR
<span class=
"hl opt">,
</span> eVaf
<span class=
"hl opt">::
</span>PswGen
<span class=
"hl opt">::
</span>Generator
<span class=
"hl opt">::
</span>Module
<span class=
"hl opt">)
</span></pre>
103 <p>We make our life easier with several
<tt>using namespace
</tt> keywords:
</p>
105 <pre class=
"hl"><span class=
"hl kwa">using namespace
</span> eVaf
<span class=
"hl opt">;
</span>
106 <span class=
"hl kwa">using namespace
</span> eVaf
<span class=
"hl opt">::
</span>PswGen
<span class=
"hl opt">;
</span>
107 <span class=
"hl kwa">using namespace
</span> eVaf
<span class=
"hl opt">::
</span>PswGen
<span class=
"hl opt">::
</span>Generator
<span class=
"hl opt">;
</span></pre>
109 <p>The Module class needs to instantiate the
<tt>iGenerator
</tt> interface in the constructor. We also need to set
110 the QObject's name property to the name of the plugin by combining the name of the module with the name of the class.
111 While the application would work without the name property, it makes our life much easier if the name property is set.
</p>
113 <p>Finally, we output an info message telling that the object was created. Every eVaf module and class is expected to
114 output info messages when they are created, destroyed, initialized or destroyed.
</p>
116 <pre class=
"hl">Module
<span class=
"hl opt">::
</span><span class=
"hl kwd">Module
</span><span class=
"hl opt">()
</span>
117 <span class=
"hl opt">:
</span> Plugins
<span class=
"hl opt">::
</span><span class=
"hl kwd">iPlugin
</span><span class=
"hl opt">()
</span>
118 <span class=
"hl opt">{
</span>
119 <span class=
"hl kwd">setObjectName
</span><span class=
"hl opt">(
</span><span class=
"hl kwd">QString
</span><span class=
"hl opt">(
</span><span class=
"hl str">"%
1.%
2"</span><span class=
"hl opt">).
</span><span class=
"hl kwd">arg
</span><span class=
"hl opt">(
</span>VER_MODULE_NAME_STR
<span class=
"hl opt">).
</span><span class=
"hl kwd">arg
</span><span class=
"hl opt">(
</span>__FUNCTION__
<span class=
"hl opt">));
</span>
121 mGenerator
<span class=
"hl opt">=
</span> <span class=
"hl kwa">new
</span> Internal
<span class=
"hl opt">::
</span>GeneratorImpl
<span class=
"hl opt">;
</span>
123 <span class=
"hl kwd">EVAF_INFO
</span><span class=
"hl opt">(
</span><span class=
"hl str">"%s created
"</span><span class=
"hl opt">,
</span> <span class=
"hl kwd">qPrintable
</span><span class=
"hl opt">(
</span><span class=
"hl kwd">objectName
</span><span class=
"hl opt">()));
</span>
124 <span class=
"hl opt">}
</span></pre>
126 <p>The
<tt>EVAF_INFO
</tt> macro comes from the
<tt>Common/iLogger
</tt> header file ane we need to include it:
</p>
128 <pre class=
"hl"><span class=
"hl ppc">#include
<Common/iLogger
></span></pre>
130 <p>The destructor should delete the
<tt>iGenerator
</tt> interface object, which we created in the constructor. The common
131 rule is that any resources allocated in the constructor shall be released in the destructor, preferrably in the opposite
132 order, ie. the first resource allocated in the constructor is released last in the destructor.
</tt>
134 <pre class=
"hl">Module
<span class=
"hl opt">::~
</span><span class=
"hl kwd">Module
</span><span class=
"hl opt">()
</span>
135 <span class=
"hl opt">{
</span>
136 <span class=
"hl kwa">delete
</span> mGenerator
<span class=
"hl opt">;
</span>
138 <span class=
"hl kwd">EVAF_INFO
</span><span class=
"hl opt">(
</span><span class=
"hl str">"%s destroyed
"</span><span class=
"hl opt">,
</span> <span class=
"hl kwd">qPrintable
</span><span class=
"hl opt">(
</span><span class=
"hl kwd">objectName
</span><span class=
"hl opt">()));
</span>
139 <span class=
"hl opt">}
</span></pre>
141 <p>We also need to implement
<tt>init()
</tt> and
<tt>done()
</tt> functions, which are used to initialize and finalize modules.
142 They are similar to the constructor and destructor with two major differences:
</p>
144 <li>The
<tt>init()
</tt> function can fail and return
<tt>false
</tt> to indicate a failure. eVaf does not use exceptions and
145 this is the only way for a module to fail without terminating the whole application. A failed module will be disabled
146 and the rest of the application can still run if it can.
</li>
147 <li>eVaf modules are loaded in two steps. At first, all the modules are created, which means that all the objects
148 are constructed. Only then will eVaf call
<tt>init()
</tt> functions meaning that when the
<tt>init()
</tt> function is
149 called, all the modules are already loaded and instantiated. Interfaces and resources from other modules that might be not
150 available when the object is created, are available when the
<tt>init()
</tt> function is called.
</li>
153 <p>The rule for
<tt>init()
</tt> and
<tt>done()
</tt> functions is the same than for constructors and destructors -- any resource
154 allocated in the
<tt>init()
</tt> function shall be released in the
<tt>done()
</tt> function and preferrably in the opposite
157 <p>This simple module needs no extra resources to be allocated and our
<tt>init()
</tt> and
<tt>done()
</tt> functions can be
160 <pre class=
"hl"><span class=
"hl kwb">bool
</span> Module
<span class=
"hl opt">::
</span><span class=
"hl kwd">init
</span><span class=
"hl opt">(
</span>QString
<span class=
"hl kwb">const
</span> <span class=
"hl opt">&</span> args
<span class=
"hl opt">)
</span>
161 <span class=
"hl opt">{
</span>
162 <span class=
"hl kwd">Q_UNUSED
</span><span class=
"hl opt">(
</span>args
<span class=
"hl opt">);
</span>
164 <span class=
"hl kwd">EVAF_INFO
</span><span class=
"hl opt">(
</span><span class=
"hl str">"%s initialized
"</span><span class=
"hl opt">,
</span> <span class=
"hl kwd">qPrintable
</span><span class=
"hl opt">(
</span><span class=
"hl kwd">objectName
</span><span class=
"hl opt">()));
</span>
166 <span class=
"hl kwa">return true
</span><span class=
"hl opt">;
</span>
167 <span class=
"hl opt">}
</span>
169 <span class=
"hl kwb">void
</span> Module
<span class=
"hl opt">::
</span><span class=
"hl kwd">done
</span><span class=
"hl opt">()
</span>
170 <span class=
"hl opt">{
</span>
171 <span class=
"hl kwd">EVAF_INFO
</span><span class=
"hl opt">(
</span><span class=
"hl str">"%s finalized
"</span><span class=
"hl opt">,
</span> <span class=
"hl kwd">qPrintable
</span><span class=
"hl opt">(
</span><span class=
"hl kwd">objectName
</span><span class=
"hl opt">()));
</span>
172 <span class=
"hl opt">}
</span></pre>
174 <p>We continue by implementing the
<tt>iGenerator
</tt> interface. There are no resources to be allocated in the constructor
175 and we just set the QObject's name property and output the info message.
</p>
177 <p>We also register the
<tt>iGenerator
</tt> interface in the global registry so that other modules can query for it and
178 use our interface. This is done by using the
<tt>Common::iRegistry
</tt> interface, which we need to include:
</p>
180 <pre class=
"hl"><span class=
"hl ppc">#include
<Common/iRegistry
></span></pre>
182 <p>The
<tt>GeneratorImpl
</tt> class was declared in the
<tt>eVaf::PswGen::Generator::Internal
</tt> namespace, so we need
183 another
<tt>using namespace
</tt> keyword before the implementation of the class:
</p>
185 <pre class=
"hl"><span class=
"hl kwa">using namespace
</span> eVaf
<span class=
"hl opt">::
</span>PswGen
<span class=
"hl opt">::
</span>Generator
<span class=
"hl opt">::
</span>Internal
<span class=
"hl opt">;
</span>
187 GeneratorImpl
<span class=
"hl opt">::
</span><span class=
"hl kwd">GeneratorImpl
</span><span class=
"hl opt">()
</span>
188 <span class=
"hl opt">:
</span> <span class=
"hl kwd">iGenerator
</span><span class=
"hl opt">()
</span>
189 <span class=
"hl opt">{
</span>
190 <span class=
"hl kwd">setObjectName
</span><span class=
"hl opt">(
</span><span class=
"hl kwd">QString
</span><span class=
"hl opt">(
</span><span class=
"hl str">"%
1.iGenerator
"</span><span class=
"hl opt">).
</span><span class=
"hl kwd">arg
</span><span class=
"hl opt">(
</span>VER_MODULE_NAME_STR
<span class=
"hl opt">));
</span>
192 Common
<span class=
"hl opt">::
</span>iRegistry
<span class=
"hl opt">::
</span><span class=
"hl kwd">instance
</span><span class=
"hl opt">()-
></span><span class=
"hl kwd">registerInterface
</span><span class=
"hl opt">(
</span><span class=
"hl str">"iGenerator
"</span><span class=
"hl opt">,
</span> <span class=
"hl kwa">this
</span><span class=
"hl opt">);
</span>
194 <span class=
"hl kwd">EVAF_INFO
</span><span class=
"hl opt">(
</span><span class=
"hl str">"%s created
"</span><span class=
"hl opt">,
</span> <span class=
"hl kwd">qPrintable
</span><span class=
"hl opt">(
</span><span class=
"hl kwd">objectName
</span><span class=
"hl opt">()));
</span>
195 <span class=
"hl opt">}
</span>
197 GeneratorImpl
<span class=
"hl opt">::~
</span><span class=
"hl kwd">GeneratorImpl
</span><span class=
"hl opt">()
</span>
198 <span class=
"hl opt">{
</span>
199 <span class=
"hl kwd">EVAF_INFO
</span><span class=
"hl opt">(
</span><span class=
"hl str">"%s destroyed
"</span><span class=
"hl opt">,
</span> <span class=
"hl kwd">qPrintable
</span><span class=
"hl opt">(
</span><span class=
"hl kwd">objectName
</span><span class=
"hl opt">()));
</span>
200 <span class=
"hl opt">}
</span></pre>
202 <p>Finally, we write the
<tt>generatePassword
</tt> function that does the actual job of the module.
</p>
204 <p>We use the MD5 cryptographic hash function to calculate a hash value over the
<tt>name
</tt> and
<tt>masterPassword
</tt> values.
205 The result, which is a binary blob, needs to be convert into something that can be used as a password and we use base
64 encoding
206 for this and cut the result to the requested length:
</p>
208 <pre class=
"hl">QString GeneratorImpl
<span class=
"hl opt">::
</span><span class=
"hl kwd">generatePassword
</span><span class=
"hl opt">(
</span>QString
<span class=
"hl kwb">const
</span> <span class=
"hl opt">&</span> name
<span class=
"hl opt">,
</span> QString
<span class=
"hl kwb">const
</span> <span class=
"hl opt">&</span> masterPassword
<span class=
"hl opt">,
</span> <span class=
"hl kwb">int
</span> length
<span class=
"hl opt">,
</span> uint flags
<span class=
"hl opt">)
</span> <span class=
"hl kwb">const
</span>
209 <span class=
"hl opt">{
</span>
210 <span class=
"hl kwd">Q_UNUSED
</span><span class=
"hl opt">(
</span>flags
<span class=
"hl opt">);
</span>
212 QByteArray inputString
<span class=
"hl opt">=
</span> <span class=
"hl kwd">QString
</span><span class=
"hl opt">(
</span><span class=
"hl str">"%
1%
2"</span><span class=
"hl opt">).
</span><span class=
"hl kwd">arg
</span><span class=
"hl opt">(
</span>name
<span class=
"hl opt">).
</span><span class=
"hl kwd">arg
</span><span class=
"hl opt">(
</span>masterPassword
<span class=
"hl opt">).
</span><span class=
"hl kwd">toLatin1
</span><span class=
"hl opt">();
</span>
213 QCryptographicHash
<span class=
"hl kwd">hash
</span><span class=
"hl opt">(
</span>QCryptographicHash
<span class=
"hl opt">::
</span>Md5
<span class=
"hl opt">);
</span>
214 hash
<span class=
"hl opt">.
</span><span class=
"hl kwd">addData
</span><span class=
"hl opt">(
</span>inputString
<span class=
"hl opt">);
</span>
215 QByteArray result
<span class=
"hl opt">=
</span> hash
<span class=
"hl opt">.
</span><span class=
"hl kwd">result
</span><span class=
"hl opt">().
</span><span class=
"hl kwd">toBase64
</span><span class=
"hl opt">();
</span>
216 <span class=
"hl kwa">if
</span> <span class=
"hl opt">(
</span>length
<span class=
"hl opt">></span> <span class=
"hl num">0</span><span class=
"hl opt">)
</span>
217 <span class=
"hl kwa">return
</span> result
<span class=
"hl opt">.
</span><span class=
"hl kwd">left
</span><span class=
"hl opt">(
</span>length
<span class=
"hl opt">);
</span>
218 <span class=
"hl kwa">else
</span>
219 <span class=
"hl kwa">return
</span> result
<span class=
"hl opt">;
</span>
220 <span class=
"hl opt">}
</span></pre>
222 <p>We also know now the maximum length of the generated password, which is
24. Go back to the
<tt>module.h
</tt> header file and
223 modify the
<tt>GeneratorImpl::maxLength()
</tt> function:
</p>
225 <pre class=
"hl"><span class=
"hl kwc">virtual
</span> <span class=
"hl kwb">int
</span> <span class=
"hl kwd">maxLength
</span><span class=
"hl opt">()
</span> <span class=
"hl kwb">const
</span> <span class=
"hl opt">{
</span> <span class=
"hl kwa">return
</span> <span class=
"hl num">24</span><span class=
"hl opt">; }
</span></pre>
227 <p>Here is the final
<tt>module.cpp
</tt> file:
</p>
229 <pre class=
"hl"><span class=
"hl com">/**
</span>
230 <span class=
"hl com"> *
@file src/apps/PswGen/Generator/module.cpp
</span>
231 <span class=
"hl com"> */
</span>
233 <span class=
"hl ppc">#include
</span> <span class=
"hl pps">"module.h
"</span><span class=
"hl ppc"></span>
234 <span class=
"hl ppc">#include
</span> <span class=
"hl pps">"version.h
"</span><span class=
"hl ppc"></span>
236 <span class=
"hl ppc">#include
<Common/iLogger
></span>
237 <span class=
"hl ppc">#include
<Common/iRegistry
></span>
239 <span class=
"hl ppc">#include
<QtCore
></span>
241 <span class=
"hl kwd">VER_EXPORT_VERSION_INFO
</span><span class=
"hl opt">()
</span>
242 <span class=
"hl kwd">Q_EXPORT_PLUGIN2
</span><span class=
"hl opt">(
</span>VER_MODULE_NAME_STR
<span class=
"hl opt">,
</span> eVaf
<span class=
"hl opt">::
</span>PswGen
<span class=
"hl opt">::
</span>Generator
<span class=
"hl opt">::
</span>Module
<span class=
"hl opt">)
</span>
244 <span class=
"hl kwa">using namespace
</span> eVaf
<span class=
"hl opt">;
</span>
245 <span class=
"hl kwa">using namespace
</span> eVaf
<span class=
"hl opt">::
</span>PswGen
<span class=
"hl opt">;
</span>
246 <span class=
"hl kwa">using namespace
</span> eVaf
<span class=
"hl opt">::
</span>PswGen
<span class=
"hl opt">::
</span>Generator
<span class=
"hl opt">;
</span>
248 Module
<span class=
"hl opt">::
</span><span class=
"hl kwd">Module
</span><span class=
"hl opt">()
</span>
249 <span class=
"hl opt">:
</span> Plugins
<span class=
"hl opt">::
</span><span class=
"hl kwd">iPlugin
</span><span class=
"hl opt">()
</span>
250 <span class=
"hl opt">{
</span>
251 <span class=
"hl kwd">setObjectName
</span><span class=
"hl opt">(
</span><span class=
"hl kwd">QString
</span><span class=
"hl opt">(
</span><span class=
"hl str">"%
1.%
2"</span><span class=
"hl opt">).
</span><span class=
"hl kwd">arg
</span><span class=
"hl opt">(
</span>VER_MODULE_NAME_STR
<span class=
"hl opt">).
</span><span class=
"hl kwd">arg
</span><span class=
"hl opt">(
</span>__FUNCTION__
<span class=
"hl opt">));
</span>
253 mGenerator
<span class=
"hl opt">=
</span> <span class=
"hl kwa">new
</span> Internal
<span class=
"hl opt">::
</span>GeneratorImpl
<span class=
"hl opt">;
</span>
255 <span class=
"hl kwd">EVAF_INFO
</span><span class=
"hl opt">(
</span><span class=
"hl str">"%s created
"</span><span class=
"hl opt">,
</span> <span class=
"hl kwd">qPrintable
</span><span class=
"hl opt">(
</span><span class=
"hl kwd">objectName
</span><span class=
"hl opt">()));
</span>
256 <span class=
"hl opt">}
</span>
258 Module
<span class=
"hl opt">::~
</span><span class=
"hl kwd">Module
</span><span class=
"hl opt">()
</span>
259 <span class=
"hl opt">{
</span>
260 <span class=
"hl kwa">delete
</span> mGenerator
<span class=
"hl opt">;
</span>
262 <span class=
"hl kwd">EVAF_INFO
</span><span class=
"hl opt">(
</span><span class=
"hl str">"%s destroyed
"</span><span class=
"hl opt">,
</span> <span class=
"hl kwd">qPrintable
</span><span class=
"hl opt">(
</span><span class=
"hl kwd">objectName
</span><span class=
"hl opt">()));
</span>
263 <span class=
"hl opt">}
</span>
265 <span class=
"hl kwb">bool
</span> Module
<span class=
"hl opt">::
</span><span class=
"hl kwd">init
</span><span class=
"hl opt">(
</span>QString
<span class=
"hl kwb">const
</span> <span class=
"hl opt">&</span> args
<span class=
"hl opt">)
</span>
266 <span class=
"hl opt">{
</span>
267 <span class=
"hl kwd">Q_UNUSED
</span><span class=
"hl opt">(
</span>args
<span class=
"hl opt">);
</span>
269 <span class=
"hl kwd">EVAF_INFO
</span><span class=
"hl opt">(
</span><span class=
"hl str">"%s initialized
"</span><span class=
"hl opt">,
</span> <span class=
"hl kwd">qPrintable
</span><span class=
"hl opt">(
</span><span class=
"hl kwd">objectName
</span><span class=
"hl opt">()));
</span>
271 <span class=
"hl kwa">return true
</span><span class=
"hl opt">;
</span>
272 <span class=
"hl opt">}
</span>
274 <span class=
"hl kwb">void
</span> Module
<span class=
"hl opt">::
</span><span class=
"hl kwd">done
</span><span class=
"hl opt">()
</span>
275 <span class=
"hl opt">{
</span>
276 <span class=
"hl kwd">EVAF_INFO
</span><span class=
"hl opt">(
</span><span class=
"hl str">"%s finalized
"</span><span class=
"hl opt">,
</span> <span class=
"hl kwd">qPrintable
</span><span class=
"hl opt">(
</span><span class=
"hl kwd">objectName
</span><span class=
"hl opt">()));
</span>
277 <span class=
"hl opt">}
</span>
279 <span class=
"hl kwa">using namespace
</span> eVaf
<span class=
"hl opt">::
</span>PswGen
<span class=
"hl opt">::
</span>Generator
<span class=
"hl opt">::
</span>Internal
<span class=
"hl opt">;
</span>
281 GeneratorImpl
<span class=
"hl opt">::
</span><span class=
"hl kwd">GeneratorImpl
</span><span class=
"hl opt">()
</span>
282 <span class=
"hl opt">:
</span> <span class=
"hl kwd">iGenerator
</span><span class=
"hl opt">()
</span>
283 <span class=
"hl opt">{
</span>
284 <span class=
"hl kwd">setObjectName
</span><span class=
"hl opt">(
</span><span class=
"hl kwd">QString
</span><span class=
"hl opt">(
</span><span class=
"hl str">"%
1.iGenerator
"</span><span class=
"hl opt">).
</span><span class=
"hl kwd">arg
</span><span class=
"hl opt">(
</span>VER_MODULE_NAME_STR
<span class=
"hl opt">));
</span>
286 Common
<span class=
"hl opt">::
</span>iRegistry
<span class=
"hl opt">::
</span><span class=
"hl kwd">instance
</span><span class=
"hl opt">()-
></span><span class=
"hl kwd">registerInterface
</span><span class=
"hl opt">(
</span><span class=
"hl str">"iGenerator
"</span><span class=
"hl opt">,
</span> <span class=
"hl kwa">this
</span><span class=
"hl opt">);
</span>
288 <span class=
"hl kwd">EVAF_INFO
</span><span class=
"hl opt">(
</span><span class=
"hl str">"%s created
"</span><span class=
"hl opt">,
</span> <span class=
"hl kwd">qPrintable
</span><span class=
"hl opt">(
</span><span class=
"hl kwd">objectName
</span><span class=
"hl opt">()));
</span>
289 <span class=
"hl opt">}
</span>
291 GeneratorImpl
<span class=
"hl opt">::~
</span><span class=
"hl kwd">GeneratorImpl
</span><span class=
"hl opt">()
</span>
292 <span class=
"hl opt">{
</span>
293 <span class=
"hl kwd">EVAF_INFO
</span><span class=
"hl opt">(
</span><span class=
"hl str">"%s destroyed
"</span><span class=
"hl opt">,
</span> <span class=
"hl kwd">qPrintable
</span><span class=
"hl opt">(
</span><span class=
"hl kwd">objectName
</span><span class=
"hl opt">()));
</span>
294 <span class=
"hl opt">}
</span>
296 QString GeneratorImpl
<span class=
"hl opt">::
</span><span class=
"hl kwd">generatePassword
</span><span class=
"hl opt">(
</span>QString
<span class=
"hl kwb">const
</span> <span class=
"hl opt">&</span> name
<span class=
"hl opt">,
</span> QString
<span class=
"hl kwb">const
</span> <span class=
"hl opt">&</span> masterPassword
<span class=
"hl opt">,
</span> <span class=
"hl kwb">int
</span> length
<span class=
"hl opt">,
</span> uint flags
<span class=
"hl opt">)
</span> <span class=
"hl kwb">const
</span>
297 <span class=
"hl opt">{
</span>
298 <span class=
"hl kwd">Q_UNUSED
</span><span class=
"hl opt">(
</span>flags
<span class=
"hl opt">);
</span>
300 QByteArray inputString
<span class=
"hl opt">=
</span> <span class=
"hl kwd">QString
</span><span class=
"hl opt">(
</span><span class=
"hl str">"%
1%
2"</span><span class=
"hl opt">).
</span><span class=
"hl kwd">arg
</span><span class=
"hl opt">(
</span>name
<span class=
"hl opt">).
</span><span class=
"hl kwd">arg
</span><span class=
"hl opt">(
</span>masterPassword
<span class=
"hl opt">).
</span><span class=
"hl kwd">toLatin1
</span><span class=
"hl opt">();
</span>
301 QCryptographicHash
<span class=
"hl kwd">hash
</span><span class=
"hl opt">(
</span>QCryptographicHash
<span class=
"hl opt">::
</span>Md5
<span class=
"hl opt">);
</span>
302 hash
<span class=
"hl opt">.
</span><span class=
"hl kwd">addData
</span><span class=
"hl opt">(
</span>inputString
<span class=
"hl opt">);
</span>
303 QByteArray result
<span class=
"hl opt">=
</span> hash
<span class=
"hl opt">.
</span><span class=
"hl kwd">result
</span><span class=
"hl opt">().
</span><span class=
"hl kwd">toBase64
</span><span class=
"hl opt">();
</span>
304 <span class=
"hl kwa">if
</span> <span class=
"hl opt">(
</span>length
<span class=
"hl opt">></span> <span class=
"hl num">0</span><span class=
"hl opt">)
</span>
305 <span class=
"hl kwa">return
</span> result
<span class=
"hl opt">.
</span><span class=
"hl kwd">left
</span><span class=
"hl opt">(
</span>length
<span class=
"hl opt">);
</span>
306 <span class=
"hl kwa">else
</span>
307 <span class=
"hl kwa">return
</span> result
<span class=
"hl opt">;
</span>
308 <span class=
"hl opt">}
</span></pre>
310 <p>Next --
<a href=
"pswgen05.html">05 - Building Generator Module
</a>.
</p>