1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <memory>
21 #include <comphelper/unique_disposing_ptr.hxx>
22 #include <comphelper/processfactory.hxx>
23 
24 #include <iderdll.hxx>
25 #include "iderdll2.hxx"
26 #include <iderid.hxx>
27 #include <basidesh.hxx>
28 #include <strings.hrc>
29 #include "basdoc.hxx"
30 #include "basicmod.hxx"
31 
32 #include <svl/srchitem.hxx>
33 #include <svx/svxids.hrc>
34 #include <com/sun/star/frame/Desktop.hpp>
35 #include <com/sun/star/script/XLibraryContainerPassword.hpp>
36 #include <unotools/resmgr.hxx>
37 #include <vcl/settings.hxx>
38 #include <sfx2/app.hxx>
39 #include <osl/diagnose.h>
40 
41 namespace basctl
42 {
43 
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::uno;
46 
47 namespace
48 {
49 
50 class Dll
51 {
52     Shell* m_pShell;
53     std::unique_ptr<ExtraData> m_xExtraData;
54 
55 public:
56     Dll ();
57 
GetShell() const58     Shell* GetShell() const { return m_pShell; }
SetShell(Shell * pShell)59     void SetShell (Shell* pShell) { m_pShell = pShell; }
60     ExtraData* GetExtraData ();
61 };
62 
63 // Holds a basctl::Dll and release it on exit, or dispose of the
64 //default XComponent, whichever comes first
65 class DllInstance : public comphelper::unique_disposing_solar_mutex_reset_ptr<Dll>
66 {
67 public:
DllInstance()68     DllInstance() : comphelper::unique_disposing_solar_mutex_reset_ptr<Dll>(Reference<lang::XComponent>( frame::Desktop::create(comphelper::getProcessComponentContext()), UNO_QUERY_THROW), new Dll, true)
69     { }
70 };
71 
72 struct theDllInstance : public rtl::Static<DllInstance, theDllInstance> { };
73 
74 } // namespace
75 
EnsureIde()76 void EnsureIde ()
77 {
78     theDllInstance::get();
79 }
80 
GetShell()81 Shell* GetShell ()
82 {
83     if (Dll* pDll = theDllInstance::get().get())
84         return pDll->GetShell();
85     return nullptr;
86 }
87 
ShellCreated(Shell * pShell)88 void ShellCreated (Shell* pShell)
89 {
90     Dll* pDll = theDllInstance::get().get();
91     if (pDll && !pDll->GetShell())
92         pDll->SetShell(pShell);
93 }
94 
ShellDestroyed(Shell const * pShell)95 void ShellDestroyed (Shell const * pShell)
96 {
97     Dll* pDll = theDllInstance::get().get();
98     if (pDll && pDll->GetShell() == pShell)
99         pDll->SetShell(nullptr);
100 }
101 
GetExtraData()102 ExtraData* GetExtraData()
103 {
104     if (Dll* pDll = theDllInstance::get().get())
105         return pDll->GetExtraData();
106     return nullptr;
107 }
108 
IDEResId(const char * pId)109 OUString IDEResId(const char *pId)
110 {
111     return Translate::get(pId, SfxApplication::GetModule(SfxToolsModule::Basic)->GetResLocale());
112 }
113 
114 namespace
115 {
116 
Dll()117 Dll::Dll () :
118     m_pShell(nullptr)
119 {
120     SfxObjectFactory& rFactory = DocShell::Factory();
121 
122     auto pModule = std::make_unique<Module>("basctl", &rFactory);
123     SfxModule* pMod = pModule.get();
124     SfxApplication::SetModule(SfxToolsModule::Basic, std::move(pModule));
125 
126     GetExtraData(); // to cause GlobalErrorHdl to be set
127 
128     rFactory.SetDocumentServiceName( "com.sun.star.script.BasicIDE" );
129 
130     DocShell::RegisterInterface( pMod );
131     Shell::RegisterFactory( SVX_INTERFACE_BASIDE_VIEWSH );
132     Shell::RegisterInterface( pMod );
133 }
134 
GetExtraData()135 ExtraData* Dll::GetExtraData ()
136 {
137     if (!m_xExtraData)
138         m_xExtraData.reset(new ExtraData);
139     return m_xExtraData.get();
140 }
141 
142 } // namespace
143 
144 
145 // basctl::ExtraData
146 
147 
ExtraData()148 ExtraData::ExtraData () :
149     bChoosingMacro(false),
150     bShellInCriticalSection(false)
151 {
152     StarBASIC::SetGlobalBreakHdl(LINK(this, ExtraData, GlobalBasicBreakHdl));
153 }
154 
~ExtraData()155 ExtraData::~ExtraData ()
156 {
157     // Resetting ErrorHdl is cleaner indeed but this instance is destroyed
158     // pretty late, after the last Basic, anyway.
159     // Due to the call there is AppData created then though and not
160     // destroyed anymore => MLK's at Purify
161 //  StarBASIC::SetGlobalErrorHdl( Link() );
162 //  StarBASIC::SetGlobalBreakHdl( Link() );
163 //  StarBASIC::setGlobalStarScriptListener( XEngineListenerRef() );
164 }
165 
IMPL_STATIC_LINK(ExtraData,GlobalBasicBreakHdl,StarBASIC *,pBasic,BasicDebugFlags)166 IMPL_STATIC_LINK(ExtraData, GlobalBasicBreakHdl, StarBASIC *, pBasic, BasicDebugFlags)
167 {
168     BasicDebugFlags nRet = BasicDebugFlags::NONE;
169     if (Shell* pShell = GetShell())
170     {
171         if (BasicManager* pBasMgr = FindBasicManager(pBasic))
172         {
173             // I do get here twice if Step into protected Basic
174             // => bad, if password query twice, also you don't see
175             // the lib in the PasswordDlg...
176             // => start no password query at this point
177             ScriptDocument aDocument( ScriptDocument::getDocumentForBasicManager( pBasMgr ) );
178             OSL_ENSURE( aDocument.isValid(), "basctl::ExtraData::GlobalBasicBreakHdl: no document for the basic manager!" );
179             if ( aDocument.isValid() )
180             {
181                 OUString aOULibName( pBasic->GetName() );
182                 Reference< script::XLibraryContainer > xModLibContainer = aDocument.getLibraryContainer( E_SCRIPTS );
183                 if ( xModLibContainer.is() && xModLibContainer->hasByName( aOULibName ) )
184                 {
185                     Reference< script::XLibraryContainerPassword > xPasswd( xModLibContainer, UNO_QUERY );
186                     if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aOULibName ) && !xPasswd->isLibraryPasswordVerified( aOULibName ) )
187                     {
188                            // a step-out should get me out of the protected area...
189                         nRet = BasicDebugFlags::StepOut;
190                     }
191                     else
192                     {
193                         nRet = pShell->CallBasicBreakHdl( pBasic );
194                     }
195                 }
196             }
197         }
198     }
199 
200     return nRet;
201 }
202 
203 
204 } // namespace basctl
205 
206 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
207