/src/libreoffice/framework/source/jobs/joburl.cxx
Line | Count | Source |
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 <sal/config.h> |
21 | | |
22 | | #include <cstring> |
23 | | |
24 | | #include <jobs/joburl.hxx> |
25 | | |
26 | | #include <vcl/svapp.hxx> |
27 | | #include <o3tl/string_view.hxx> |
28 | | |
29 | | namespace framework{ |
30 | | |
31 | | /** |
32 | | @short special ctor |
33 | | @descr It initialize this new instance with a (hopefully) valid job URL. |
34 | | This URL will be parsed. After that we set our members right, |
35 | | so other interface methods of this class can be used to get |
36 | | all items of this URL. Of course it will be possible to know, |
37 | | if this URL was valid too. |
38 | | |
39 | | @param sURL |
40 | | the job URL for parsing |
41 | | */ |
42 | | JobURL::JobURL( /*IN*/ const OUString& sURL ) |
43 | 0 | { |
44 | 0 | m_eRequest = E_UNKNOWN; |
45 | | |
46 | | // syntax: vnd.sun.star.job:{[event=<name>],[alias=<name>],[service=<name>]} |
47 | | |
48 | | // check for "vnd.sun.star.job:" |
49 | 0 | if (!sURL.startsWithIgnoreAsciiCase("vnd.sun.star.job:")) |
50 | 0 | return; |
51 | | |
52 | 0 | sal_Int32 t = std::strlen("vnd.sun.star.job:"); |
53 | 0 | do |
54 | 0 | { |
55 | | // separate all token of "{[event=<name>],[alias=<name>],[service=<name>]}" |
56 | 0 | OUString sToken = sURL.getToken(0, JOBURL_PART_SEPARATOR, t); |
57 | 0 | OUString sPartValue; |
58 | 0 | OUString sPartArguments; |
59 | | |
60 | | // check for "event=" |
61 | 0 | if ( |
62 | 0 | (JobURL::implst_split(sToken,JOBURL_EVENT_STR,JOBURL_EVENT_LEN,sPartValue,sPartArguments)) && |
63 | 0 | (!sPartValue.isEmpty()) |
64 | 0 | ) |
65 | 0 | { |
66 | | // set the part value |
67 | 0 | m_sEvent = sPartValue; |
68 | 0 | m_eRequest |= E_EVENT; |
69 | 0 | } |
70 | 0 | else |
71 | | // check for "alias=" |
72 | 0 | if ( |
73 | 0 | (JobURL::implst_split(sToken,JOBURL_ALIAS_STR,JOBURL_ALIAS_LEN,sPartValue,sPartArguments)) && |
74 | 0 | (!sPartValue.isEmpty()) |
75 | 0 | ) |
76 | 0 | { |
77 | | // set the part value |
78 | 0 | m_sAlias = sPartValue; |
79 | 0 | m_eRequest |= E_ALIAS; |
80 | 0 | } |
81 | 0 | else |
82 | | // check for "service=" |
83 | 0 | if ( |
84 | 0 | (JobURL::implst_split(sToken,JOBURL_SERVICE_STR,JOBURL_SERVICE_LEN,sPartValue,sPartArguments)) && |
85 | 0 | (!sPartValue.isEmpty()) |
86 | 0 | ) |
87 | 0 | { |
88 | | // set the part value |
89 | 0 | m_sService = sPartValue; |
90 | 0 | m_eRequest |= E_SERVICE; |
91 | 0 | } |
92 | 0 | } |
93 | 0 | while(t!=-1); |
94 | 0 | } |
95 | | |
96 | | /** |
97 | | @short knows, if this job URL object hold a valid URL inside |
98 | | |
99 | | @return <TRUE/> if it represent a valid job URL. |
100 | | */ |
101 | | bool JobURL::isValid() const |
102 | 0 | { |
103 | 0 | return (m_eRequest!=E_UNKNOWN); |
104 | 0 | } |
105 | | |
106 | | /** |
107 | | @short get the event item of this job URL |
108 | | @descr Because the three possible parts of such URL (event, alias, service) |
109 | | can't be combined, this method can(!) return a valid value - but it's |
110 | | not a must. That's why the return value must be used too, to detect a missing |
111 | | event value. |
112 | | |
113 | | @param sEvent |
114 | | returns the possible existing event value |
115 | | e.g. "vnd.sun.star.job:event=myEvent" returns "myEvent" |
116 | | |
117 | | @return <TRUE/> if an event part of the job URL exist and the out parameter |
118 | | sEvent was filled. |
119 | | |
120 | | @attention The out parameter will be reset every time. Don't use it if method returns <FALSE/>! |
121 | | */ |
122 | | bool JobURL::getEvent( /*OUT*/ OUString& sEvent ) const |
123 | 0 | { |
124 | 0 | sEvent.clear(); |
125 | 0 | bool bSet = ((m_eRequest & E_EVENT) == E_EVENT); |
126 | 0 | if (bSet) |
127 | 0 | sEvent = m_sEvent; |
128 | |
|
129 | 0 | return bSet; |
130 | 0 | } |
131 | | |
132 | | /** |
133 | | @short get the alias item of this job URL |
134 | | @descr Because the three possible parts of such URL (event, alias, service) |
135 | | can't be combined, this method can(!) return a valid value - but it's |
136 | | not a must. that's why the return value must be used too, to detect a missing |
137 | | alias value. |
138 | | |
139 | | @param sAlias |
140 | | returns the possible existing alias value |
141 | | e.g. "vnd.sun.star.job:alias=myAlias" returns "myAlias" |
142 | | |
143 | | @return <TRUE/> if an alias part of the job URL exist and the out parameter |
144 | | sAlias was filled. |
145 | | |
146 | | @attention The out parameter will be reset every time. Don't use it if method returns <FALSE/>! |
147 | | */ |
148 | | bool JobURL::getAlias( /*OUT*/ OUString& sAlias ) const |
149 | 0 | { |
150 | 0 | sAlias.clear(); |
151 | 0 | bool bSet = ((m_eRequest & E_ALIAS) == E_ALIAS); |
152 | 0 | if (bSet) |
153 | 0 | sAlias = m_sAlias; |
154 | |
|
155 | 0 | return bSet; |
156 | 0 | } |
157 | | |
158 | | /** |
159 | | @short get the service item of this job URL |
160 | | @descr Because the three possible parts of such URL (event, service, service) |
161 | | can't be combined, this method can(!) return a valid value - but it's |
162 | | not a must. That's why the return value must be used too, to detect a missing |
163 | | service value. |
164 | | |
165 | | @param sAlias |
166 | | returns the possible existing service value |
167 | | e.g. "vnd.sun.star.job:service=com.sun.star.Service" returns "com.sun.star.Service" |
168 | | |
169 | | @return <TRUE/> if a service part of the job URL exist and the out parameter |
170 | | sService was filled. |
171 | | |
172 | | @attention The out parameter will be reset every time. Don't use it if method returns <FALSE/>! |
173 | | */ |
174 | | bool JobURL::getService( /*OUT*/ OUString& sService ) const |
175 | 0 | { |
176 | 0 | sService.clear(); |
177 | 0 | bool bSet = ((m_eRequest & E_SERVICE) == E_SERVICE); |
178 | 0 | if (bSet) |
179 | 0 | sService = m_sService; |
180 | |
|
181 | 0 | return bSet; |
182 | 0 | } |
183 | | |
184 | | /** |
185 | | @short searches for a special identifier in the given string and split it |
186 | | @descr If the given identifier could be found at the beginning of the given string, |
187 | | this method split it into different parts and return it. |
188 | | Following schema is used: <partidentifier>=<partvalue>[?<partarguments>] |
189 | | |
190 | | @param sPart |
191 | | the string, which should be analyzed |
192 | | |
193 | | @param pPartIdentifier |
194 | | the part identifier value, which must be found at the beginning of the |
195 | | parameter <var>sPart</var> |
196 | | |
197 | | @param nPartLength |
198 | | the length of the ascii value <var>pPartIdentifier</var> |
199 | | |
200 | | @param rPartValue |
201 | | returns the part value if <var>sPart</var> was split successfully |
202 | | |
203 | | @param rPartArguments |
204 | | returns the part arguments if <var>sPart</var> was split successfully |
205 | | |
206 | | @return <TRUE/> if the identifier could be found and the string was split. |
207 | | <FALSE/> otherwise. |
208 | | */ |
209 | | bool JobURL::implst_split( /*IN*/ std::u16string_view sPart , |
210 | | /*IN*/ const char* pPartIdentifier , |
211 | | /*IN*/ sal_Int32 nPartLength , |
212 | | /*OUT*/ OUString& rPartValue , |
213 | | /*OUT*/ OUString& rPartArguments ) |
214 | 0 | { |
215 | | // first search for the given identifier |
216 | 0 | bool bPartFound = o3tl::matchIgnoreAsciiCase(sPart, std::string_view(pPartIdentifier,nPartLength)); |
217 | | |
218 | | // If it exist - we can split the part and return sal_True. |
219 | | // Otherwise we do nothing and return sal_False. |
220 | 0 | if (bPartFound) |
221 | 0 | { |
222 | | // But may the part has optional arguments - separated by a "?". |
223 | | // Do so - we set the return value with the whole part string. |
224 | | // Arguments will be set to an empty string as default. |
225 | | // If we detect the right sign - we split the arguments and overwrite the default. |
226 | 0 | std::u16string_view sValueAndArguments = sPart.substr(nPartLength); |
227 | 0 | std::u16string_view sValue = sValueAndArguments; |
228 | 0 | OUString sArguments; |
229 | |
|
230 | 0 | size_t nArgStart = sValueAndArguments.find('?'); |
231 | 0 | if (nArgStart != std::u16string_view::npos) |
232 | 0 | { |
233 | 0 | sValue = sValueAndArguments.substr(0,nArgStart); |
234 | 0 | ++nArgStart; // ignore '?'! |
235 | 0 | sArguments = sValueAndArguments.substr(nArgStart); |
236 | 0 | } |
237 | |
|
238 | 0 | rPartValue = sValue; |
239 | 0 | rPartArguments = sArguments; |
240 | 0 | } |
241 | |
|
242 | 0 | return bPartFound; |
243 | 0 | } |
244 | | |
245 | | } // namespace framework |
246 | | |
247 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |