root/swishctl/src/CSwishCtl.cpp

Revision 1287, 10.2 kB (checked in by augur, 5 years ago)

Changed SwishCtl::Init(IndexFiles?) so the IndexFiles? parameter works as documented.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*
2
3  * $Id$
4
5  *
6
7  * SwishCtl - SWISH-E API ActiveX Control
8
9  * Copyright (c) 2003 Peoples Resource Centre Wellington NZ
10
11  *
12
13  * This program is free software; you can redistribute it and/or modify
14
15  * it under the terms of the GNU General Public License as published by
16
17  * the Free Software Foundation; either version 2 of the License, or
18
19  * (at your option) any later version.
20
21  *
22
23  * This program is distributed in the hope that it will be useful,
24
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
29  * GNU General Public License for more details.
30
31  *
32
33  * You should have received a copy of the GNU General Public License
34
35  * along with this program; if not, write to the Free Software
36
37  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
38
39  */
40
41
42
43 // CSwishCtl.cpp : Implementation of CCSwishCtl
44
45 // N.B. can't add swish-e/src to build settings for this project
46
47 // because the folder contains a string.h which overrides the
48
49 // default search location for string.h !
50
51 #include "stdafx.h"
52
53 #include <string>
54
55 using namespace std;
56
57
58
59 #include "swish-e.h"
60
61 #include "SwishCtl.h"
62
63 #include "CSwishCtl.h"
64
65 #include "AnsiBSTR.h"
66
67 #include "regkey.h"
68
69
70
71 /////////////////////////////////////////////////////////////////////////////
72
73 // CCSwishCtl
74
75 CCSwishCtl::CCSwishCtl()
76
77 {
78
79         swish_handle = NULL;
80
81         swish_results = NULL;
82
83         current_result = NULL;
84
85 }
86
87
88
89 CCSwishCtl::~CCSwishCtl(void)
90
91 {
92
93        
94
95         if ( swish_handle ) {
96
97         SwishClose( swish_handle );  /* free the memory used */       
98
99         }
100
101 }
102
103
104
105 // I didn't know where to put this prototype...
106
107 // so it can go here for the meantime
108
109 bool GetModulePath(char filename[ _MAX_PATH ]);
110
111
112
113
114
115 STDMETHODIMP CCSwishCtl::Init(BSTR IndexFiles)
116
117 {
118         // In order to claim "safe for scripting",
119         // don't allow location of resources to be specified
120         // as a parameter - for this application, the dll must
121         // and the index files must reside in the same folder
122         // So now the IndexFiles parameter is the name of a
123         // registry value in the options key for this app
124     CAnsiStr bstr_indexfiles(IndexFiles);
125
126         const char registrykey[] = "Software\\SWISH-E Team\\SwishCtl\\Options";
127
128
129         RegKey hkcu(HKEY_LOCAL_MACHINE) ;
130
131
132         string indexfilepath;
133
134         string indexfilename;
135
136
137
138
139
140         if (hkcu.OpenKey( registrykey, KEY_QUERY_VALUE )) {
141
142                 indexfilepath = hkcu.QueryValue( "IndexLocation"  );
143
144                 indexfilename = hkcu.QueryValue( bstr_indexfiles.c_str() );
145
146         }
147
148                
149
150
151
152         chdir( indexfilepath.c_str() );
153
154
155
156         swish_handle = SwishInit((char *)indexfilename.c_str());
157
158        
159
160         if ( ! swish_handle ) {
161
162                 ::MessageBox( NULL, "SwishInit call failed...", indexfilepath.c_str(), MB_OK );
163
164                 return E_FAIL;
165
166         }
167
168     if ( SwishError( swish_handle ) )    {
169
170                 ::MessageBox( NULL, indexfilepath.c_str(), "Swish-e Index File error...", MB_OK );
171
172
173
174         SwishClose( swish_handle );  /* free the memory used */       
175
176                 return E_FAIL;
177
178     }
179
180
181
182         swish_search = NULL;
183
184
185
186         return S_OK;
187
188 }
189
190
191
192
193
194 STDMETHODIMP CCSwishCtl::GetInterfaceSafetyOptions(REFIID riid, DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions)
195
196 {
197
198    ATLTRACE(_T("CObjectSafetyImpl::GetInterfaceSafetyOptions\n"));
199
200    if (!pdwSupportedOptions || !pdwEnabledOptions)
201
202            return E_FAIL;
203
204    LPUNKNOWN pUnk;
205
206    if (_InternalQueryInterface (riid, (void**)&pUnk) == E_NOINTERFACE)
207
208    {
209
210       // Our object doesn't even support this interface.
211
212       return E_NOINTERFACE;   
213
214    } else {
215
216            // Cleanup after ourselves.
217
218        pUnk->Release();     
219
220            pUnk = NULL;   
221
222    }   
223
224    if (riid == IID_IDispatch) {
225
226       // IDispatch is an interface used for scripting. If your
227
228       // control supports other IDispatch or Dual interfaces, you
229
230       // may decide to add them here as well. Client wants to know
231
232       // if object is safe for scripting. Only indicate safe for
233
234       // scripting when the interface is safe.
235
236       *pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
237
238       *pdwEnabledOptions = m_dwSafety &
239
240                            INTERFACESAFE_FOR_UNTRUSTED_CALLER;     
241
242           return S_OK;
243
244    } else if ((riid == IID_IPersistStreamInit) ||
245
246              (riid == IID_IPersistStorage))
247
248    {
249
250       // IID_IPersistStreamInit and IID_IPersistStorage are
251
252       // interfaces used for Initialization. If your control
253
254       // supports other Persistence interfaces, you may decide to
255
256       // add them here as well. Client wants to know if object is
257
258       // safe for initializing. Only indicate safe for initializing
259
260       // when the interface is safe.
261
262       *pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
263
264       *pdwEnabledOptions = m_dwSafety &
265
266                            INTERFACESAFE_FOR_UNTRUSTED_DATA;
267
268       return S_OK;
269
270    } else {
271
272           // We are saying that no other interfaces in this control are
273
274       // safe for initializing or scripting.     
275
276           *pdwSupportedOptions = 0;
277
278       *pdwEnabledOptions = 0;     
279
280           return E_FAIL;   
281
282         }
283
284 }
285
286
287
288 STDMETHODIMP CCSwishCtl::SetInterfaceSafetyOptions(REFIID riid, DWORD dwOptionSetMask, DWORD dwEnabledOptions)
289
290 {
291
292    ATLTRACE(_T("CObjectSafetyImpl::SetInterfaceSafetyOptions\n"));
293
294    if (!dwOptionSetMask && !dwEnabledOptions) return E_FAIL;   
295
296
297
298    LPUNKNOWN pUnk;
299
300    if (_InternalQueryInterface (riid, (void**)&pUnk) == E_NOINTERFACE)
301
302    {
303
304       // Our object doesn't even support this interface.
305
306       return E_NOINTERFACE;   
307
308    } else {
309
310            // Cleanup after ourselves.
311
312        pUnk->Release();     
313
314            pUnk = NULL;   
315
316    }
317
318    // Store our current safety level to return in   
319
320    // GetInterfaceSafetyOptions
321
322    m_dwSafety |= dwEnabledOptions & dwOptionSetMask;
323
324    if ((riid == IID_IDispatch) &&
325
326        (m_dwSafety & INTERFACESAFE_FOR_UNTRUSTED_CALLER))
327
328    {
329
330       // Client wants us to disable any functionality that would
331
332       // make the control unsafe for scripting. The same applies to
333
334       // any other IDispatch or Dual interfaces your control may
335
336       // support. Because our control is safe for scripting by
337
338       // default we just return S_OK.
339
340           return S_OK;
341
342    } else if (((riid == IID_IPersistStreamInit) ||
343
344               (riid == IID_IPersistStorage)) &&
345
346              (m_dwSafety & INTERFACESAFE_FOR_UNTRUSTED_DATA))
347
348    {
349
350       // Client wants us to make the control safe for initializing
351
352       // from persistent data. For these interfaces, this control
353
354       // is safe so we return S_OK. For Any interfaces that are not
355
356       // safe, we would return E_FAIL.     
357
358            return S_OK;   
359
360    } else {
361
362       // This control doesn't allow Initialization or Scripting
363
364       // from any other interfaces so return E_FAIL.     
365
366            return E_FAIL;   
367
368         }   
369
370 }
371
372
373
374
375
376 STDMETHODIMP CCSwishCtl::Error(int *errcode)
377
378 {
379
380         *errcode = SwishError( swish_handle );
381
382         return S_OK;
383
384 }
385
386
387
388
389
390 STDMETHODIMP CCSwishCtl::Query(BSTR words, int *errcode)
391
392 {
393
394
395
396         HRESULT hresult = NewSearch( words, errcode );
397
398
399
400         if ( hresult != S_OK ) return hresult;
401
402
403
404         return Execute( NULL, errcode );       
405
406 }
407
408
409
410 STDMETHODIMP CCSwishCtl::ErrorString(BSTR *errstring)
411
412 {
413
414         if ( ! swish_handle ) {
415
416                 return E_FAIL;
417
418         }
419
420
421
422         char *err = SwishErrorString(swish_handle);
423
424        
425
426         CAnsiBSTR bstrTemp( err );
427
428
429
430     if (!bstrTemp)
431
432            return E_OUTOFMEMORY;
433
434  
435
436         *errstring = bstrTemp.Detach();
437
438         return S_OK;
439
440 }
441
442
443
444 STDMETHODIMP CCSwishCtl::Hits(int *hits)
445
446 {
447
448         if (! swish_results ) return E_FAIL;
449
450         *hits = SwishHits( swish_results );
451
452         return S_OK;
453
454 }
455
456
457
458 STDMETHODIMP CCSwishCtl::NextResult(int *more)
459
460 {
461
462         *more = FALSE;
463
464         if (!swish_results) return E_FAIL;
465
466
467
468         current_result = SwishNextResult( swish_results );
469
470         if ( current_result ) *more = TRUE;
471
472        
473
474         return S_OK;
475
476 }
477
478
479
480
481
482 STDMETHODIMP CCSwishCtl::ResultPropertyString(
483
484                                         BSTR propertyname, BSTR *result)
485
486 {
487
488
489
490
491
492         CAnsiStr bstr_convert(propertyname);
493
494
495
496         char *propertyval = SwishResultPropertyStr(
497
498                 current_result, bstr_convert.c_str());
499
500
501
502         CAnsiBSTR bstrTemp( propertyval );
503
504
505
506     if (!bstrTemp)
507
508            return E_OUTOFMEMORY;
509
510  
511
512         *result = bstrTemp.Detach();
513
514
515
516         return S_OK;
517
518
519
520 }
521
522
523
524 STDMETHODIMP CCSwishCtl::Close()
525
526 {
527
528         current_result = NULL;
529
530         if ( swish_handle ) {
531
532                 if ( swish_results ) {
533
534                         Free_Results_Object( swish_results );
535
536                         swish_results = NULL;
537
538                 }
539
540                 if (swish_search) {
541
542                         Free_Search_Object( swish_search );
543
544                         swish_search = NULL;
545
546                 }
547
548         SwishClose( swish_handle );  /* free the memory used */       
549
550                 swish_handle = NULL;
551
552         }
553
554
555
556         return S_OK;
557
558 }
559
560
561
562 STDMETHODIMP CCSwishCtl::SeekResult(int position, int *result)
563
564 {
565
566         *result = -1; // an error
567
568         if (!swish_results) return S_OK;
569
570
571
572         *result = SwishSeekResult( swish_results, position );
573
574        
575
576         return S_OK;
577
578 }
579
580
581
582 STDMETHODIMP CCSwishCtl::SetSort(BSTR sort)
583
584 {
585
586         if ( ! (swish_handle && swish_search) ){
587
588                 return E_FAIL;
589
590         }
591
592
593
594         CAnsiStr bstr_sort( sort );
595
596
597
598         SwishSetSort( swish_search, bstr_sort.c_str() );
599
600
601
602         return S_OK;
603
604 }
605
606
607
608
609
610 STDMETHODIMP CCSwishCtl::NewSearch(BSTR query, int *errcode)
611
612 {
613
614         *errcode = -1;
615
616         if ( ! swish_handle ) {
617
618                 ::MessageBox( NULL, "swish_handle is null...", "New Search", MB_OK );
619
620                 return E_FAIL;
621
622         }
623
624         CAnsiStr bstr_query( query );
625
626
627
628         if ( swish_results ) {
629
630                 Free_Results_Object(swish_results);
631
632                 swish_results = NULL;
633
634         }
635
636
637
638         if ( swish_search ) {
639
640                 Free_Search_Object( swish_search );
641
642         }
643
644         swish_search = New_Search_Object( swish_handle, bstr_query.c_str() );
645
646
647
648
649
650         SwishResetSearchLimit( swish_search );
651
652
653
654         if ( ! swish_search ) {
655
656                 ::MessageBox( NULL, "swish_search is null...", "Error", MB_OK );
657
658         }
659
660
661
662         *errcode = SwishError( swish_handle );
663
664
665
666         return S_OK;
667
668 }
669
670
671
672 STDMETHODIMP CCSwishCtl::Execute(BSTR words, int *errcode)
673
674 {
675
676         *errcode = -1;
677
678         if ( ! swish_handle ) {
679
680                
681
682                 return E_FAIL;
683
684         }
685
686
687
688         if ( !words ) {
689
690                 // use existing query string - no need to convert string
691
692                 swish_results = SwishExecute( swish_search, NULL );
693
694                 *errcode = SwishError( swish_handle );
695
696                 return S_OK;
697
698         }       
699
700         CAnsiStr bstr_words( words );
701
702         swish_results = SwishExecute( swish_search, bstr_words.c_str() );
703
704
705
706         *errcode = SwishError( swish_handle );
707
708
709
710         return S_OK;
711
712 }
713
714
715
716
717
718
719
720 STDMETHODIMP CCSwishCtl::SetSearchLimit(BSTR propertyname, BSTR low, BSTR hi)
721
722 {
723
724         if ( ! (swish_handle && swish_search) ){
725
726                 return E_FAIL;
727
728         }
729
730         CAnsiStr bstr_propertyname(propertyname);
731
732         CAnsiStr bstr_low(low);
733
734         CAnsiStr bstr_hi(hi);
735
736
737
738         SwishSetSearchLimit( swish_search,
739
740                 bstr_propertyname.c_str(),
741
742                 bstr_low.c_str(),
743
744                 bstr_hi.c_str() );
745
746         return S_OK;
747
748 }
749
750
751
Note: See TracBrowser for help on using the browser.