-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathWApplication.C
More file actions
1959 lines (1642 loc) · 51.6 KB
/
WApplication.C
File metadata and controls
1959 lines (1642 loc) · 51.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2008 Emweb bv, Herent, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include <fstream>
#include "Wt/Utils.h"
#include "Wt/WApplication.h"
#include "Wt/WCombinedLocalizedStrings.h"
#include "Wt/WContainerWidget.h"
#include "Wt/WCssTheme.h"
#include "Wt/WDate.h"
#include "Wt/WDefaultLoadingIndicator.h"
#include "Wt/WException.h"
#include "Wt/WFileUpload.h"
#include "Wt/WLinkedCssStyleSheet.h"
#include "Wt/WMemoryResource.h"
#include "Wt/WServer.h"
#include "Wt/WTimer.h"
#ifndef WT_TARGET_JAVA
#include "Wt/WWebSocketResource.h"
#endif // WT_TARGET_JAVA
#include "Wt/Http/Cookie.h"
#include "WebSession.h"
#include "DomElement.h"
#include "Configuration.h"
#include "SoundManager.h"
#include "WebController.h"
#include "WebUtils.h"
#include "ServerSideFontMetrics.h"
#include <boost/algorithm/string/predicate.hpp>
#include <boost/pool/pool.hpp>
#ifdef min
#undef min
#endif
namespace skeletons {
extern const char* Wt_strings_xml;
extern const char* Wt_templates_xml;
}
namespace Wt {
LOGGER("WApplication");
#if !(defined(DOXYGEN_ONLY) || defined(WT_TARGET_JAVA))
const WtLibVersion WT_INCLUDED_VERSION = WtLibVersion();
#endif
const char *WApplication::RESOURCES_URL = "resourcesURL";
MetaHeader::MetaHeader(MetaHeaderType aType,
const std::string& aName,
const WString& aContent,
const std::string& aLang,
const std::string& aUserAgent)
: type(aType), name(aName), lang(aLang), userAgent(aUserAgent),
content(aContent)
{ }
WApplication::ScriptLibrary::ScriptLibrary(const std::string& anUri,
const std::string& aSymbol)
: uri(anUri), symbol(aSymbol)
{ }
WApplication::MetaLink::MetaLink(const std::string &aHref,
const std::string &aRel,
const std::string &aMedia,
const std::string &aHreflang,
const std::string &aType,
const std::string &aSizes,
bool aDisabled)
: href(aHref), rel(aRel), media(aMedia), hreflang(aHreflang), type(aType),
sizes(aSizes), disabled(aDisabled)
{ }
bool WApplication::ScriptLibrary::operator< (const ScriptLibrary& other) const
{
return uri < other.uri;
}
bool WApplication::ScriptLibrary::operator== (const ScriptLibrary& other) const
{
return uri == other.uri;
}
WApplication::WApplication(const WEnvironment& env
#if !(defined(DOXYGEN_ONLY) || defined(WT_TARGET_JAVA))
, WtLibVersion
#endif
)
: session_(env.session_),
#ifndef WT_CNOR
weakSession_(session_->shared_from_this()),
#endif // WT_CNOR
titleChanged_(false),
closeMessageChanged_(false),
localeChanged_(false),
widgetRoot_(nullptr),
timerRoot_(nullptr),
serverPush_(0),
serverPushChanged_(true),
#ifndef WT_CNOR
eventSignalPool_(new boost::pool<>(sizeof(EventSignal<>))),
#endif // WT_CNOR
javaScriptClass_("Wt"),
quitted_(false),
internalPathsEnabled_(false),
exposedOnly_(nullptr),
loadingIndicator_(nullptr),
bodyHtmlClassChanged_(true),
enableAjax_(false),
#ifndef WT_TARGET_JAVA
initialized_(false),
#endif // WT_TARGET_JAVA
selectionStart_(-1),
selectionEnd_(-1),
layoutDirection_(LayoutDirection::LeftToRight),
htmlAttributeChanged_(true),
bodyAttributeChanged_(true),
scriptLibrariesAdded_(0),
theme_(nullptr),
styleSheetsAdded_(0),
exposeSignals_(true),
newBeforeLoadJavaScript_(0),
autoJavaScriptChanged_(false),
#ifndef WT_DEBUG_JS
newJavaScriptPreamble_(0),
#endif // WT_DEBUG_JS
customJQuery_(false),
showLoadingIndicator_("showload", this),
hideLoadingIndicator_("hideload", this),
unloaded_(this, "Wt-unload"),
idleTimeout_(this, "Wt-idleTimeout"),
soundManager_(nullptr),
serverSideFontMetrics_(nullptr)
{
session_->setApplication(this);
locale_ = environment().locale();
renderedInternalPath_ = newInternalPath_ = environment().internalPath();
internalPathIsChanged_ = false;
internalPathDefaultValid_ = true;
internalPathValid_ = true;
theme_.reset(new WCssTheme("default"));
#ifndef WT_TARGET_JAVA
setLocalizedStrings(std::make_shared<WMessageResourceBundle>());
#else
setLocalizedStrings(std::shared_ptr<WLocalizedStrings>());
#endif // !WT_TARGET_JAVA
if (!environment().javaScript() && environment().agentIsIE()) {
/*
* WARNING: Similar code in WebRenderer.C must be kept in sync for
* plain boot.
*/
if (static_cast<unsigned int>(environment().agent()) <
static_cast<unsigned int>(UserAgent::IE9)) {
const Configuration& conf = environment().server()->configuration();
bool selectIE7 = conf.uaCompatible().find("IE8=IE7")
!= std::string::npos;
if (selectIE7)
addMetaHeader(MetaHeaderType::HttpHeader, "X-UA-Compatible", "IE=7");
} else if (environment().agent() == UserAgent::IE9) {
addMetaHeader(MetaHeaderType::HttpHeader, "X-UA-Compatible", "IE=9");
} else if (environment().agent() == UserAgent::IE10) {
addMetaHeader(MetaHeaderType::HttpHeader, "X-UA-Compatible", "IE=10");
} else {
addMetaHeader(MetaHeaderType::HttpHeader, "X-UA-Compatible", "IE=11");
}
}
domRoot_.reset(new WContainerWidget());
domRoot_->setGlobalUnfocused(true);
domRoot_->setStyleClass("Wt-domRoot");
domRoot_->load();
if (session_->type() == EntryPointType::Application)
domRoot_->resize(WLength::Auto, WLength(100, LengthUnit::Percentage));
timerRoot_ = domRoot_->addWidget(std::make_unique<WContainerWidget>());
timerRoot_->setId("Wt-timers");
timerRoot_->resize(WLength::Auto, 0);
timerRoot_->setPositionScheme(PositionScheme::Absolute);
if (session_->type() == EntryPointType::Application) {
widgetRoot_ = domRoot_->addWidget(std::make_unique<WContainerWidget>());
widgetRoot_->resize(WLength::Auto, WLength(100, LengthUnit::Percentage));
} else {
domRoot2_.reset(new WContainerWidget());
domRoot2_->load();
}
// a define so that it shouts at us !
#define RTL ".Wt-rtl "
/*
* Subset of typical CSS "reset" styles, only those that are needed
* for Wt's built-in widgets and are relatively harmless.
*/
styleSheet_.addRule("table", "border-collapse: collapse; border: 0px;"
"border-spacing: 0px");
styleSheet_.addRule("div, td, img",
"margin: 0px; padding: 0px; border: 0px");
styleSheet_.addRule("td", "vertical-align: top;");
styleSheet_.addRule("td", "text-align: left;");
styleSheet_.addRule(RTL "td", "text-align: right;");
styleSheet_.addRule("button", "white-space: nowrap;");
styleSheet_.addRule("video", "display: block");
if (environment().agentIsGecko())
styleSheet_.addRule("html", "overflow: auto;");
/*
* Standard Wt CSS styles: resources, button wrap and form validation
*/
styleSheet_.addRule("iframe.Wt-resource",
"width: 0px; height: 0px; border: 0px;");
if (environment().agentIsIElt(9))
styleSheet_.addRule("iframe.Wt-shim",
"position: absolute; top: -1px; left: -1px; "
"z-index: -1;"
"opacity: 0; filter: alpha(opacity=0);"
"border: none; margin: 0; padding: 0;");
styleSheet_.addRule(".Wt-wrap",
"border: 0px;"
"margin: 0px;"
"padding: 0px;"
"font: inherit; "
"cursor: pointer;"
"background: transparent;"
"text-decoration: none;"
"color: inherit;");
styleSheet_.addRule(".Wt-wrap", "text-align: left;");
styleSheet_.addRule(RTL ".Wt-wrap", "text-align: right;");
styleSheet_.addRule("div.Wt-chwrap", "width: 100%; height: 100%");
if (environment().agentIsIE())
styleSheet_.addRule(".Wt-wrap",
"margin: -1px 0px -3px;");
//styleSheet_.addRule("a.Wt-wrap", "text-decoration: none;");
styleSheet_.addRule(".unselectable",
"-moz-user-select:-moz-none;"
"-khtml-user-select: none;"
"-webkit-user-select: none;"
"user-select: none;");
styleSheet_.addRule(".selectable",
"-moz-user-select: text;"
"-khtml-user-select: normal;"
"-webkit-user-select: text;"
"user-select: text;");
styleSheet_.addRule(".Wt-domRoot", "position: relative;");
styleSheet_.addRule("body.Wt-layout", std::string() +
"height: 100%; width: 100%;"
"margin: 0px; padding: 0px; border: none;"
+ (environment().javaScript() ? "overflow:hidden" : ""));
styleSheet_.addRule("html.Wt-layout", std::string() +
"height: 100%; width: 100%;"
"margin: 0px; padding: 0px; border: none;"
+ (environment().javaScript() ? "overflow:hidden" : ""));
if (environment().agentIsOpera())
if (environment().userAgent().find("Mac OS X") != std::string::npos)
styleSheet_.addRule("img.Wt-indeterminate", "margin: 4px 1px -3px 2px;");
else
styleSheet_.addRule("img.Wt-indeterminate", "margin: 4px 2px -3px 0px;");
else
if (environment().userAgent().find("Mac OS X") != std::string::npos)
styleSheet_.addRule("img.Wt-indeterminate", "margin: 4px 3px 0px 4px;");
else
styleSheet_.addRule("img.Wt-indeterminate", "margin: 3px 3px 0px 4px;");
if (environment().supportsCss3Animations()) {
std::string prefix = "";
if (environment().agentIsWebKit())
prefix = "webkit-";
else if (environment().agentIsGecko())
prefix = "moz-";
useStyleSheet(WApplication::relativeResourcesUrl()
+ prefix + "transitions.css");
}
setLoadingIndicator
(std::unique_ptr<WLoadingIndicator>(new WDefaultLoadingIndicator()));
unloaded_.connect(this, &WApplication::doUnload);
idleTimeout_.connect(this, &WApplication::doIdleTimeout);
}
void WApplication::setJavaScriptClass(const std::string& javaScriptClass)
{
if (session_->type() != EntryPointType::Application)
javaScriptClass_ = javaScriptClass;
}
void WApplication
::setLoadingIndicator(std::unique_ptr<WLoadingIndicator> indicator)
{
#ifdef WT_TARGET_JAVA
if (!loadingIndicator_) {
showLoadingIndicator_.connect(showLoadJS);
hideLoadingIndicator_.connect(hideLoadJS);
}
#endif
if (loadingIndicator_)
loadingIndicator_->removeFromParent();
loadingIndicator_ = indicator.get();
if (loadingIndicator_) {
domRoot_->addWidget(std::move(indicator));
#ifndef WT_TARGET_JAVA
showLoadingIndicator_.connect(loadingIndicator_, &WWidget::show);
hideLoadingIndicator_.connect(loadingIndicator_, &WWidget::hide);
#else
// stateless learning does not work in Java
showLoadJS.setJavaScript
("function(o,e) {"
"" WT_CLASS ".inline('" + loadingIndicator_->id() + "');"
"}");
hideLoadJS.setJavaScript
("function(o,e) {"
"" WT_CLASS ".hide('" + loadingIndicator_->id() + "');"
"}");
#endif
loadingIndicator_->hide();
}
}
#ifndef WT_TARGET_JAVA
void WApplication::initialize()
{ }
void WApplication::finalize()
{ }
#else
void WApplication::destroy()
{ }
#endif // !WT_TARGET_JAVA
#ifndef WT_TARGET_JAVA
WMessageResourceBundle& WApplication::messageResourceBundle()
{
auto result = dynamic_cast<WMessageResourceBundle*>(localizedStrings().get());
if (result)
return *result;
else
throw WException("messageResourceBundle(): failed to cast localizedStrings() to WMessageResourceBundle*!");
}
#endif // !WT_TARGET_JAVA
std::string WApplication::onePixelGifUrl()
{
if (environment().agentIsIElt(7)) {
if (!onePixelGifR_) {
std::unique_ptr<WMemoryResource> w(new WMemoryResource("image/gif"));
static const unsigned char gifData[]
= { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00,
0x80, 0x00, 0x00, 0xdb, 0xdf, 0xef, 0x00, 0x00, 0x00, 0x21,
0xf9, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44,
0x01, 0x00, 0x3b };
w->setData(gifData, 43);
onePixelGifR_ = std::move(w);
}
return onePixelGifR_->url();
} else
return "data:image/gif;base64,"
"R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
}
WApplication::~WApplication()
{
#ifndef WT_TARGET_JAVA
Configuration& conf = env().server()->configuration();
if (conf.servePrivateResourcesToBots() && env().agentIsSpiderBot()) {
exposeBotResources();
}
// Fix issue #5331: if WTimer is a child of WApplication,
// it will outlive timerRoot_. Delete it now already.
for (std::size_t i = 0; i < children_.size(); ++i) {
WTimer *timer = dynamic_cast<WTimer*>(children_[i].get());
if (timer) {
removeChild(timer);
}
}
timerRoot_ = nullptr;
// First remove all children owned by this WApplication (issue #6282)
if (domRoot_) {
auto domRoot = domRoot_.get();
for (WWidget *child : domRoot->children())
removeChild(child);
}
if (domRoot2_) {
auto domRoot = domRoot2_.get();
for (WWidget *child : domRoot->children())
removeChild(child);
}
#endif // WT_TARGET_JAVA
/* Clear domRoot */
domRoot_.reset();
/* Widgetset bound widgets */
domRoot2_.reset();
session_->setApplication(nullptr);
#ifndef WT_TARGET_JAVA
delete eventSignalPool_;
#endif
}
WWebWidget *WApplication::domRoot() const
{
return domRoot_.get();
}
ServerSideFontMetrics *WApplication::serverSideFontMetrics()
{
if (!serverSideFontMetrics_)
serverSideFontMetrics_.reset(new ServerSideFontMetrics());
return serverSideFontMetrics_.get();
}
void WApplication::attachThread(bool attach)
{
#ifndef WT_CNOR
if (attach) {
std::shared_ptr<WebSession> session = weakSession_.lock();
if (session)
WebSession::Handler::attachThreadToSession(session);
else
session_->attachThreadToLockedHandler();
} else
WebSession::Handler::attachThreadToSession(std::shared_ptr<WebSession>());
#else
if (attach)
WebSession::Handler::attachThreadToSession(session_);
else
WebSession::Handler::attachThreadToSession(std::shared_ptr<WebSession>());
#endif
}
std::string WApplication::relativeResourcesUrl()
{
#ifndef WT_TARGET_JAVA
std::string result = "resources/";
readConfigurationProperty(WApplication::RESOURCES_URL, result);
if (!result.empty() && result[result.length()-1] != '/')
result += '/';
return result;
#else
WApplication *app = WApplication::instance();
const Configuration& conf = app->environment().server()->configuration();
const std::string* path = conf.property(WApplication::RESOURCES_URL);
int version;
try {
version = app->environment().server()->servletMajorVersion();
} catch (std::exception& e) {
return "";
}
if (version < 3) {
/*
* Arghll... we should in fact know when we need the absolute URL: only
* when we are having a request.pathInfo().
*/
if (path == "/wt-resources/") {
std::string result = app->environment().deploymentPath();
if (!result.empty() && result[result.length() - 1] == '/')
return result + path->substr(1);
else
return result + *path;
} else
return *path;
} else { // from v3.0, resources can be deployed in META-INF of a jar-file
std::string contextPath = app->environment().server()->getContextPath();
if (!contextPath.empty() && contextPath[contextPath.length() - 1] != '/')
contextPath = contextPath + "/";
if (path == "/wt-resources/")
return contextPath + path->substr(1);
else
return contextPath + *path;
}
#endif // WT_TARGET_JAVA
}
std::string WApplication::resourcesUrl()
{
return WApplication::instance()->resolveRelativeUrl
(WApplication::relativeResourcesUrl());
}
#ifndef WT_TARGET_JAVA
std::string WApplication::appRoot()
{
return WServer::instance()->appRoot();
}
std::string WApplication::docRoot() const
{
return environment().getCgiValue("DOCUMENT_ROOT");
}
void WApplication::setConnectionMonitor(const std::string& jsFunction) {
doJavaScript(javaScriptClass_
+ "._p_.setConnectionMonitor("+ jsFunction + ")");
}
#endif // WT_TARGET_JAVA
void WApplication::bindWidget(std::unique_ptr<WWidget> widget,
const std::string& domId)
{
if (session_->type() != EntryPointType::WidgetSet)
throw WException("WApplication::bindWidget() can be used only "
"in WidgetSet mode.");
widget->setId(domId);
widget->setJavaScriptMember("wtReparentBarrier", "true");
domRoot2_->addWidget(std::move(widget));
}
void WApplication::pushExposedConstraint(WWidget *w)
{
exposedOnly_ = w;
}
void WApplication::popExposedConstraint(WWidget *w)
{
assert (exposedOnly_ == w);
exposedOnly_ = nullptr;
}
void WApplication::addGlobalWidget(WWidget *w)
{
domRoot_->addWidget(std::unique_ptr<WWidget>(w)); // take ownership
#ifndef WT_TARGET_JAVA
domRoot_->removeChild(w).release(); // return ownership
#endif // WT_TARGET_JAVA
w->setGlobalWidget(true);
}
void WApplication::removeGlobalWidget(WWidget *w)
{
// In the destructor domRoot_->reset() can cause domRoot_
// to be null. In that case, we don't need to remove this
// widget from the domRoot.
if (domRoot_) {
w->setGlobalWidget(false);
auto removed = domRoot_->removeWidget(w);
// domRoot_ should never own the global widget
#ifndef WT_TARGET_JAVA
assert(!removed);
#endif // WT_TARGET_JAVA
}
}
bool WApplication::isExposed(WWidget *w) const
{
// File uploads may be hidden when emitting a signal.
// Other hidden widgets should not emit signals.
// FIXME: fix all of the regressions caused by this check
// before reenabling it
#if 0
if (!w->isVisible() && !dynamic_cast<WFileUpload*>(w))
return false;
#endif
if (!w->isEnabled())
return false;
if (w == domRoot_.get())
return true;
if (w->parent() == timerRoot_)
return true;
if (exposedOnly_)
return exposedOnly_->isExposed(w);
else {
WWidget *p = w->adam();
return (p == domRoot_.get() || p == domRoot2_.get());
}
}
std::string WApplication::sessionId() const
{
return session_->sessionId();
}
#ifndef WT_TARGET_JAVA
void WApplication::changeSessionId()
{
session_->generateNewSessionId();
}
#endif // WT_TARGET_JAVA
void WApplication::setCssTheme(const std::string& theme)
{
setTheme(std::shared_ptr<WTheme>(new WCssTheme(theme)));
}
void WApplication::setTheme(const std::shared_ptr<WTheme>& theme)
{
theme_ = theme;
theme_->init(this);
}
void WApplication::useStyleSheet(const WLink& link, const std::string& media)
{
useStyleSheet(WLinkedCssStyleSheet(link, media));
}
void WApplication::useStyleSheet(const WLink& link,
const std::string& condition,
const std::string& media)
{
useStyleSheet(WLinkedCssStyleSheet(link, media), condition);
}
void WApplication::useStyleSheet(const WLinkedCssStyleSheet& styleSheet,
const std::string& condition)
{
bool display = true;
if (!condition.empty()) {
display = false;
if (environment().agentIsIE()) {
int thisVersion = 4;
switch (environment().agent()) {
case UserAgent::IEMobile:
thisVersion = 5; break;
case UserAgent::IE6:
thisVersion = 6; break;
case UserAgent::IE7:
thisVersion = 7; break;
case UserAgent::IE8:
thisVersion = 8; break;
case UserAgent::IE9:
thisVersion = 9; break;
case UserAgent::IE10:
thisVersion = 10; break;
default:
thisVersion = 11; break;
}
enum { lte, lt, eq, gt, gte } cond = eq;
bool invert = false;
std::string r = condition;
while (!r.empty()) {
if (r.length() >= 3 && r.substr(0, 3) == "IE ") {
r = r.substr(3);
} else if (r[0] == '!') {
r = r.substr(1);
invert = !invert;
} else if (r.length() >= 4 && r.substr(0, 4) == "lte ") {
r = r.substr(4);
cond = lte;
} else if (r.length() >= 3 && r.substr(0, 3) == "lt ") {
r = r.substr(3);
cond = lt;
} else if (r.length() >= 3 && r.substr(0, 3) == "gt ") {
r = r.substr(3);
cond = gt;
} else if (r.length() >= 4 && r.substr(0, 4) == "gte ") {
r = r.substr(4);
cond = gte;
} else {
try {
int version = Utils::stoi(r);
switch (cond) {
case eq: display = thisVersion == version; break;
case lte: display = thisVersion <= version; break;
case lt: display = thisVersion < version; break;
case gte: display = thisVersion >= version; break;
case gt: display = thisVersion > version; break;
}
if (invert)
display = !display;
} catch (std::exception& e) {
LOG_ERROR("Could not parse condition: '" << condition << "'");
}
r.clear();
}
}
}
}
if (display) {
for (unsigned i = 0; i < styleSheets_.size(); ++i) {
if (styleSheets_[i].link() == styleSheet.link()
&& styleSheets_[i].media() == styleSheet.media()) {
return;
}
}
styleSheets_.push_back(styleSheet);
++styleSheetsAdded_;
}
}
void WApplication::removeStyleSheet(const WLink& link)
{
for (int i = (int)styleSheets_.size() - 1; i > -1; --i) {
if (styleSheets_[i].link() == link) {
WLinkedCssStyleSheet &sheet = styleSheets_[i];
styleSheetsToRemove_.push_back(sheet);
if (i > (int)styleSheets_.size() + styleSheetsAdded_ - 1)
styleSheetsAdded_--;
styleSheets_.erase(styleSheets_.begin() + i);
break;
}
}
}
const WEnvironment& WApplication::environment() const
{
return session_->env();
}
WEnvironment& WApplication::env()
{
return session_->env();
}
void WApplication::setTitle(const WString& title)
{
if (session_->renderer().preLearning() || title_ != title) {
title_ = title;
titleChanged_ = true;
}
}
void WApplication::setConfirmCloseMessage(const WString& message)
{
if (message != closeMessage_) {
closeMessage_ = message;
closeMessageChanged_ = true;
}
}
std::string WApplication::url(const std::string& internalPath) const
{
return resolveRelativeUrl(session_->mostRelativeUrl(internalPath));
}
std::string WApplication::makeAbsoluteUrl(const std::string& url) const
{
return session_->makeAbsoluteUrl(url);
}
std::string WApplication::resolveRelativeUrl(const std::string& url) const
{
return session_->fixRelativeUrl(url);
}
void WApplication::quit()
{
quit(WString::tr("Wt.QuittedMessage"));
}
void WApplication::quit(const WString& restartMessage)
{
quitted_ = true;
quittedMessage_ = restartMessage;
}
WWidget *WApplication::findWidget(const std::string& name)
{
WWidget *result = domRoot_->find(name);
if (!result && domRoot2_)
result = domRoot2_->find(name);
return result;
}
void WApplication::doUnload()
{
if (session_->suspended())
return;
const Configuration& conf = environment().server()->configuration();
if (conf.reloadIsNewSession())
unload();
else
session_->setState(WebSession::State::Loaded, 5);
}
void WApplication::unload()
{
quit();
}
void WApplication::suspend(std::chrono::seconds duration) {
session_->setState(WebSession::State::Suspended, static_cast<int>(duration.count()));
}
void WApplication::doIdleTimeout()
{
const Configuration& conf = environment().server()->configuration();
if (conf.idleTimeout() != -1)
idleTimeout();
}
void WApplication::idleTimeout()
{
const Configuration& conf = environment().server()->configuration();
LOG_INFO("User idle for " << conf.idleTimeout() << " seconds, quitting due to idle timeout");
quit();
}
void WApplication::handleJavaScriptError(const std::string& errorText)
{
LOG_ERROR("JavaScript error: " << errorText);
quit();
}
void WApplication::addExposedSignal(Wt::EventSignalBase *signal)
{
std::string s = signal->encodeCmd();
Utils::insert(exposedSignals_, s, signal);
LOG_DEBUG("addExposedSignal: " << s);
}
void WApplication::removeExposedSignal(Wt::EventSignalBase *signal)
{
std::string s = signal->encodeCmd();
if (exposedSignals_.erase(s)) {
justRemovedSignals_.insert(s);
LOG_DEBUG("removeExposedSignal: " << s);
} else {
LOG_DEBUG("removeExposedSignal of non-exposed " << s << "??");
}
}
EventSignalBase *
WApplication::decodeExposedSignal(const std::string& signalName) const
{
SignalMap::const_iterator i = exposedSignals_.find(signalName);
if (i != exposedSignals_.end()) {
return i->second;
} else
return nullptr;
}
std::string WApplication::encodeSignal(const std::string& objectId,
const std::string& name) const
{
return objectId + '.' + name;
}
std::string WApplication::resourceMapKey(WResource *resource)
{
return resource->internalPath().empty()
? resource->id() : "/path/" + resource->internalPath();
}
std::string WApplication::addExposedResource(WResource *resource)
{
exposedResources_[resourceMapKey(resource)] = resource;
resource->incrementVersion();
std::string fn = resource->suggestedFileName().toUTF8();
if (!fn.empty() && fn[0] != '/')
fn = '/' + fn;
Configuration& conf = env().server()->configuration();
if (conf.servePrivateResourcesToBots() && env().agentIsSpiderBot()) {
std::string appUrl = session_->applicationUrl();
if (appUrl == "/") {
appUrl.clear();
} else if (!appUrl.empty() && appUrl[appUrl.length() - 1] != '/') {
appUrl += '/';
}
return appUrl
+ conf.botResourcesPath() + "/"
+ Utils::urlEncode(resource->botResourceId())
+ fn;
} else if (resource->internalPath().empty()) {
return session_->mostRelativeUrl(fn, true)
+ "&request=resource&resource=" + Utils::urlEncode(resource->id())
+ "&ver=" + std::to_string(resource->version());
} else {
fn = resource->internalPath() + fn;
if (!session_->applicationName().empty() && fn[0] != '/')
fn = '/' + fn;
return session_->mostRelativeUrl(fn, true);
}
}
#ifndef WT_TARGET_JAVA
void WApplication::addWebSocketResource(WWebSocketResource* webSocketResource)
{
if (environment().server()->configuration().webSockets()) {
exposedWebSocketResources_[webSocketResource->handleResource().get()] = webSocketResource;
} else {
LOG_WARN("WebSockets are disabled by config, but a WWebSocketResource is used. Resource will be unreachable.");
}
}
#endif // WT_TARGET_JAVA
bool WApplication::removeExposedResource(WResource *resource)
{
std::string key = resourceMapKey(resource);
ResourceMap::iterator i = exposedResources_.find(key);
if (i != exposedResources_.end() && i->second == resource) {
#ifndef WT_TARGET_JAVA
exposedResources_.erase(i);
#else
exposedResources_.erase(key);
#endif
return true;
} else
return false;
}
void WApplication::exposeBotResources()
{
for (auto it = exposedResources_.begin(); it != exposedResources_.end(); ++it) {
WResource *resource = it->second;
if (resource) {
std::shared_ptr<WResource> botResource = resource->botResource();
if (botResource) {
try {
WServer::instance()->addResource(botResource, resource->url());
} catch (std::exception& e) {
LOG_DEBUG("Failed to add private resource for bot at url: " << resource->url());
}
}
}
}
}
#ifndef WT_TARGET_JAVA
void WApplication::removeWebSocketResource(WWebSocketResource* webSocketResource)
{
for (auto i = exposedWebSocketResources_.begin(); i != exposedWebSocketResources_.end(); ++i) {
if (i->second == webSocketResource) {
exposedWebSocketResources_.erase(i);
}
}
}
#endif // WT_TARGET_JAVA
WResource *WApplication::decodeExposedResource(const std::string& resourceKey)
const
{
ResourceMap::const_iterator i = exposedResources_.find(resourceKey);
if (i != exposedResources_.end())
return i->second;
else {
std::size_t j = resourceKey.rfind('/');
if (j != std::string::npos && j > 1)
return decodeExposedResource(resourceKey.substr(0, j));
else
return nullptr;
}
}
#ifndef WT_TARGET_JAVA
WWebSocketResource* WApplication::findMatchingWebSocketResource(WResource* resource) const