1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #include "precompiled_sd.hxx"
23 
24 #include "MasterPageContainerFiller.hxx"
25 
26 #include "MasterPageDescriptor.hxx"
27 #include "MasterPageContainerProviders.hxx"
28 #include "TemplateScanner.hxx"
29 
30 using namespace ::com::sun::star;
31 using namespace ::com::sun::star::uno;
32 
33 
34 namespace sd { namespace sidebar {
35 
MasterPageContainerFiller(ContainerAdapter & rpAdapter)36 MasterPageContainerFiller::MasterPageContainerFiller (ContainerAdapter& rpAdapter)
37     : mrContainerAdapter(rpAdapter),
38       meState(INITIALIZE_TEMPLATE_SCANNER),
39       mpScannerTask(),
40       mpLastAddedEntry(NULL),
41       mnIndex(1)
42 {
43     // Add one entry for the default master page.  We use temporarily the
44     // DefaultPagePreviewProvider to prevent the rendering (and the
45     // expensive creation) of the default page.  It is replaced later on by
46     // another.
47     SharedMasterPageDescriptor pDescriptor (new MasterPageDescriptor(
48         MasterPageContainer::DEFAULT,
49         0,
50         String(),
51         String(),
52         String(),
53         false,
54         ::boost::shared_ptr<PageObjectProvider>(new DefaultPageObjectProvider()),
55         ::boost::shared_ptr<PreviewProvider>(new PagePreviewProvider())));
56     mrContainerAdapter.PutMasterPage(pDescriptor);
57 }
58 
59 
60 
61 
~MasterPageContainerFiller(void)62 MasterPageContainerFiller::~MasterPageContainerFiller (void)
63 {
64 }
65 
66 
67 
68 
RunNextStep(void)69 void MasterPageContainerFiller::RunNextStep (void)
70 {
71     switch (meState)
72     {
73         case INITIALIZE_TEMPLATE_SCANNER:
74             mpScannerTask.reset(new TemplateScanner());
75             meState = SCAN_TEMPLATE;
76             break;
77 
78         case SCAN_TEMPLATE:
79             meState = ScanTemplate();
80             break;
81 
82         case ADD_TEMPLATE:
83             meState = AddTemplate();
84             break;
85 
86         case DONE:
87         case ERROR:
88         default:
89             break;
90     }
91 
92     // When the state has just been set to DONE or ERROR then tell the
93     // container that no more templates will be coming and stop the
94     // scanning.
95     switch (meState)
96     {
97         case DONE:
98         case ERROR:
99             if (mpScannerTask.get() != NULL)
100             {
101                 mrContainerAdapter.FillingDone();
102                 mpScannerTask.reset();
103             }
104 		default:
105 			break;
106     }
107 }
108 
109 
110 
111 
HasNextStep(void)112 bool MasterPageContainerFiller::HasNextStep (void)
113 {
114     switch (meState)
115     {
116         case DONE:
117         case ERROR:
118             return false;
119 
120         default:
121             return true;
122     }
123 }
124 
125 
126 
127 
ScanTemplate(void)128 MasterPageContainerFiller::State MasterPageContainerFiller::ScanTemplate (void)
129 {
130     State eState (ERROR);
131 
132     if (mpScannerTask.get() != NULL)
133     {
134         if (mpScannerTask->HasNextStep())
135         {
136             mpScannerTask->RunNextStep();
137             if (mpScannerTask->GetLastAddedEntry() != mpLastAddedEntry)
138             {
139                 mpLastAddedEntry = mpScannerTask->GetLastAddedEntry();
140                 if (mpLastAddedEntry != NULL)
141                     eState = ADD_TEMPLATE;
142                 else
143                     eState = SCAN_TEMPLATE;
144             }
145             else
146                 eState = SCAN_TEMPLATE;
147         }
148         else
149             eState = DONE;
150     }
151 
152     return eState;
153 }
154 
155 
156 
157 
AddTemplate(void)158 MasterPageContainerFiller::State MasterPageContainerFiller::AddTemplate (void)
159 {
160     if (mpLastAddedEntry != NULL)
161     {
162         SharedMasterPageDescriptor pDescriptor (new MasterPageDescriptor(
163             MasterPageContainer::TEMPLATE,
164             mnIndex,
165             mpLastAddedEntry->msPath,
166             mpLastAddedEntry->msTitle,
167             String(),
168             false,
169             ::boost::shared_ptr<PageObjectProvider>(
170                 new TemplatePageObjectProvider(mpLastAddedEntry->msPath)),
171             ::boost::shared_ptr<PreviewProvider>(
172                 new TemplatePreviewProvider(mpLastAddedEntry->msPath))));
173         // For user supplied templates we use a different preview provider:
174         // The preview in the document shows not only shapes on the master
175         // page but also shapes on the foreground.  This is misleading and
176         // therefore these previews are discarded and created directly from
177         // the page objects.
178         if (pDescriptor->GetURLClassification() == MasterPageDescriptor::URLCLASS_USER)
179             pDescriptor->mpPreviewProvider = ::boost::shared_ptr<PreviewProvider>(
180                 new PagePreviewProvider());
181 
182         mrContainerAdapter.PutMasterPage(pDescriptor);
183         ++mnIndex;
184     }
185 
186     return SCAN_TEMPLATE;
187 }
188 
189 
190 
191 } } // end of namespace sd::sidebar
192