1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "fpdfsdk/include/fsdk_define.h"
8 #include "fpdfsdk/include/fpdfxfa/fpdfxfa_doc.h"
9 #include "fpdfsdk/include/fsdk_mgr.h"
10 #include "fpdfsdk/include/fpdfxfa/fpdfxfa_app.h"
11 #include "fpdfsdk/include/fpdfxfa/fpdfxfa_util.h"
12 #include "fpdfsdk/include/fpdfxfa/fpdfxfa_page.h"
13 #include "fpdfsdk/include/javascript/IJavaScript.h"
14 #include "public/fpdf_formfill.h"
15 
16 #define IDS_XFA_Validate_Input                                          \
17   "At least one required field was empty. Please fill in the required " \
18   "fields\r\n(highlighted) before continuing."
19 
20 // submit
21 #define FXFA_CONFIG 0x00000001
22 #define FXFA_TEMPLATE 0x00000010
23 #define FXFA_LOCALESET 0x00000100
24 #define FXFA_DATASETS 0x00001000
25 #define FXFA_XMPMETA 0x00010000
26 #define FXFA_XFDF 0x00100000
27 #define FXFA_FORM 0x01000000
28 #define FXFA_PDF 0x10000000
29 
30 #ifndef _WIN32
31 extern void SetLastError(int err);
32 
33 extern int GetLastError();
34 #endif
35 
CPDFXFA_Document(CPDF_Document * pPDFDoc,CPDFXFA_App * pProvider)36 CPDFXFA_Document::CPDFXFA_Document(CPDF_Document* pPDFDoc,
37                                    CPDFXFA_App* pProvider)
38     : m_iDocType(DOCTYPE_PDF),
39       m_pPDFDoc(pPDFDoc),
40       m_pSDKDoc(nullptr),
41       m_pXFADoc(nullptr),
42       m_pXFADocView(nullptr),
43       m_pApp(pProvider),
44       m_pJSContext(nullptr) {
45 }
46 
~CPDFXFA_Document()47 CPDFXFA_Document::~CPDFXFA_Document() {
48   if (m_pJSContext && m_pSDKDoc && m_pSDKDoc->GetEnv())
49     m_pSDKDoc->GetEnv()->GetJSRuntime()->ReleaseContext(m_pJSContext);
50 
51   delete m_pSDKDoc;
52 
53   if (m_pPDFDoc) {
54     CPDF_Parser* pParser = m_pPDFDoc->GetParser();
55     if (pParser)
56       delete pParser;
57     else
58       delete m_pPDFDoc;
59   }
60   if (m_pXFADoc) {
61     IXFA_App* pApp = m_pApp->GetXFAApp();
62     if (pApp) {
63       IXFA_DocHandler* pDocHandler = pApp->GetDocHandler();
64       if (pDocHandler) {
65         CloseXFADoc(pDocHandler);
66       }
67     }
68     delete m_pXFADoc;
69   }
70 }
71 
LoadXFADoc()72 FX_BOOL CPDFXFA_Document::LoadXFADoc() {
73   if (!m_pPDFDoc)
74     return FALSE;
75 
76   m_XFAPageList.RemoveAll();
77 
78   IXFA_App* pApp = m_pApp->GetXFAApp();
79   if (!pApp)
80     return FALSE;
81 
82   m_pXFADoc = pApp->CreateDoc(this, m_pPDFDoc);
83   if (!m_pXFADoc) {
84     SetLastError(FPDF_ERR_XFALOAD);
85     return FALSE;
86   }
87 
88   IXFA_DocHandler* pDocHandler = pApp->GetDocHandler();
89   if (!pDocHandler) {
90     SetLastError(FPDF_ERR_XFALOAD);
91     return FALSE;
92   }
93 
94   pDocHandler->StartLoad(m_pXFADoc);
95   int iStatus = pDocHandler->DoLoad(m_pXFADoc, NULL);
96   if (iStatus != XFA_PARSESTATUS_Done) {
97     CloseXFADoc(pDocHandler);
98     SetLastError(FPDF_ERR_XFALOAD);
99     return FALSE;
100   }
101   pDocHandler->StopLoad(m_pXFADoc);
102   pDocHandler->SetJSERuntime(m_pXFADoc, m_pApp->GetJSERuntime());
103 
104   if (pDocHandler->GetDocType(m_pXFADoc) == XFA_DOCTYPE_Dynamic)
105     m_iDocType = DOCTYPE_DYNAMIC_XFA;
106   else
107     m_iDocType = DOCTYPE_STATIC_XFA;
108 
109   m_pXFADocView = pDocHandler->CreateDocView(m_pXFADoc, XFA_DOCVIEW_View);
110   if (m_pXFADocView->StartLayout() < 0) {
111     CloseXFADoc(pDocHandler);
112     SetLastError(FPDF_ERR_XFALAYOUT);
113     return FALSE;
114   }
115 
116   m_pXFADocView->DoLayout(NULL);
117   m_pXFADocView->StopLayout();
118   return TRUE;
119 }
120 
GetPageCount()121 int CPDFXFA_Document::GetPageCount() {
122   if (!m_pPDFDoc && !m_pXFADoc)
123     return 0;
124 
125   switch (m_iDocType) {
126     case DOCTYPE_PDF:
127     case DOCTYPE_STATIC_XFA:
128       if (m_pPDFDoc)
129         return m_pPDFDoc->GetPageCount();
130     case DOCTYPE_DYNAMIC_XFA:
131       if (m_pXFADoc)
132         return m_pXFADocView->CountPageViews();
133     default:
134       return 0;
135   }
136 
137   return 0;
138 }
139 
GetPage(int page_index)140 CPDFXFA_Page* CPDFXFA_Document::GetPage(int page_index) {
141   if (!m_pPDFDoc && !m_pXFADoc)
142     return NULL;
143 
144   CPDFXFA_Page* pPage = NULL;
145   if (m_XFAPageList.GetSize()) {
146     pPage = m_XFAPageList.GetAt(page_index);
147     if (pPage)
148       pPage->AddRef();
149   } else {
150     m_XFAPageList.SetSize(GetPageCount());
151   }
152 
153   if (!pPage) {
154     pPage = new CPDFXFA_Page(this, page_index);
155     FX_BOOL bRet = pPage->LoadPage();
156     if (!bRet) {
157       delete pPage;
158       return NULL;
159     }
160 
161     m_XFAPageList.SetAt(page_index, pPage);
162   }
163 
164   return pPage;
165 }
166 
GetPage(IXFA_PageView * pPage)167 CPDFXFA_Page* CPDFXFA_Document::GetPage(IXFA_PageView* pPage) {
168   if (!pPage)
169     return NULL;
170 
171   if (!m_pXFADoc)
172     return NULL;
173 
174   if (m_iDocType != DOCTYPE_DYNAMIC_XFA)
175     return NULL;
176 
177   int nSize = m_XFAPageList.GetSize();
178   for (int i = 0; i < nSize; i++) {
179     CPDFXFA_Page* pTempPage = m_XFAPageList.GetAt(i);
180     if (!pTempPage)
181       continue;
182     if (pTempPage->GetXFAPageView() && pTempPage->GetXFAPageView() == pPage)
183       return pTempPage;
184   }
185 
186   return NULL;
187 }
188 
RemovePage(CPDFXFA_Page * page)189 void CPDFXFA_Document::RemovePage(CPDFXFA_Page* page) {
190   m_XFAPageList.SetAt(page->GetPageIndex(), NULL);
191 }
192 
GetSDKDocument(CPDFDoc_Environment * pFormFillEnv)193 CPDFSDK_Document* CPDFXFA_Document::GetSDKDocument(
194     CPDFDoc_Environment* pFormFillEnv) {
195   if (!m_pSDKDoc && pFormFillEnv)
196     m_pSDKDoc = new CPDFSDK_Document(this, pFormFillEnv);
197   return m_pSDKDoc;
198 }
199 
FXRect2PDFRect(const CFX_RectF & fxRectF,CPDF_Rect & pdfRect)200 void CPDFXFA_Document::FXRect2PDFRect(const CFX_RectF& fxRectF,
201                                       CPDF_Rect& pdfRect) {
202   pdfRect.left = fxRectF.left;
203   pdfRect.top = fxRectF.bottom();
204   pdfRect.right = fxRectF.right();
205   pdfRect.bottom = fxRectF.top;
206 }
207 
SetChangeMark(IXFA_Doc * hDoc)208 void CPDFXFA_Document::SetChangeMark(IXFA_Doc* hDoc) {
209   if (hDoc == m_pXFADoc && m_pSDKDoc) {
210     m_pSDKDoc->SetChangeMark();
211   }
212 }
213 
GetChangeMark(IXFA_Doc * hDoc)214 FX_BOOL CPDFXFA_Document::GetChangeMark(IXFA_Doc* hDoc) {
215   if (hDoc == m_pXFADoc && m_pSDKDoc)
216     return m_pSDKDoc->GetChangeMark();
217   return FALSE;
218 }
219 
InvalidateRect(IXFA_PageView * pPageView,const CFX_RectF & rt,FX_DWORD dwFlags)220 void CPDFXFA_Document::InvalidateRect(IXFA_PageView* pPageView,
221                                       const CFX_RectF& rt,
222                                       FX_DWORD dwFlags /* = 0 */) {
223   if (!m_pXFADoc || !m_pSDKDoc)
224     return;
225 
226   if (m_iDocType != DOCTYPE_DYNAMIC_XFA)
227     return;
228 
229   CPDF_Rect rcPage;
230   FXRect2PDFRect(rt, rcPage);
231 
232   CPDFXFA_Page* pPage = GetPage(pPageView);
233 
234   if (pPage == NULL)
235     return;
236 
237   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
238   if (!pEnv)
239     return;
240 
241   pEnv->FFI_Invalidate((FPDF_PAGE)pPage, rcPage.left, rcPage.bottom,
242                        rcPage.right, rcPage.top);
243 }
244 
InvalidateRect(IXFA_Widget * hWidget,FX_DWORD dwFlags)245 void CPDFXFA_Document::InvalidateRect(IXFA_Widget* hWidget,
246                                       FX_DWORD dwFlags /* = 0 */) {
247   if (!hWidget)
248     return;
249 
250   if (!m_pXFADoc || !m_pSDKDoc || !m_pXFADocView)
251     return;
252 
253   if (m_iDocType != DOCTYPE_DYNAMIC_XFA)
254     return;
255 
256   IXFA_WidgetHandler* pWidgetHandler = m_pXFADocView->GetWidgetHandler();
257   if (!pWidgetHandler)
258     return;
259 
260   IXFA_PageView* pPageView = pWidgetHandler->GetPageView(hWidget);
261   if (!pPageView)
262     return;
263 
264   CFX_RectF rect;
265   pWidgetHandler->GetRect(hWidget, rect);
266   InvalidateRect(pPageView, rect, dwFlags);
267 }
268 
DisplayCaret(IXFA_Widget * hWidget,FX_BOOL bVisible,const CFX_RectF * pRtAnchor)269 void CPDFXFA_Document::DisplayCaret(IXFA_Widget* hWidget,
270                                     FX_BOOL bVisible,
271                                     const CFX_RectF* pRtAnchor) {
272   if (!hWidget || pRtAnchor == NULL)
273     return;
274 
275   if (!m_pXFADoc || !m_pSDKDoc || !m_pXFADocView)
276     return;
277 
278   if (m_iDocType != DOCTYPE_DYNAMIC_XFA)
279     return;
280 
281   IXFA_WidgetHandler* pWidgetHandler = m_pXFADocView->GetWidgetHandler();
282   if (!pWidgetHandler)
283     return;
284 
285   IXFA_PageView* pPageView = pWidgetHandler->GetPageView(hWidget);
286   if (!pPageView)
287     return;
288 
289   CPDFXFA_Page* pPage = GetPage(pPageView);
290 
291   if (pPage == NULL)
292     return;
293 
294   CPDF_Rect rcCaret;
295   FXRect2PDFRect(*pRtAnchor, rcCaret);
296 
297   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
298   if (!pEnv)
299     return;
300 
301   pEnv->FFI_DisplayCaret((FPDF_PAGE)pPage, bVisible, rcCaret.left, rcCaret.top,
302                          rcCaret.right, rcCaret.bottom);
303 }
304 
GetPopupPos(IXFA_Widget * hWidget,FX_FLOAT fMinPopup,FX_FLOAT fMaxPopup,const CFX_RectF & rtAnchor,CFX_RectF & rtPopup)305 FX_BOOL CPDFXFA_Document::GetPopupPos(IXFA_Widget* hWidget,
306                                       FX_FLOAT fMinPopup,
307                                       FX_FLOAT fMaxPopup,
308                                       const CFX_RectF& rtAnchor,
309                                       CFX_RectF& rtPopup) {
310   if (NULL == hWidget) {
311     return FALSE;
312   }
313   IXFA_PageView* pXFAPageView =
314       m_pXFADocView->GetWidgetHandler()->GetPageView(hWidget);
315   if (NULL == pXFAPageView) {
316     return FALSE;
317   }
318   CPDFXFA_Page* pPage = GetPage(pXFAPageView);
319   if (pPage == NULL)
320     return FALSE;
321 
322   CXFA_WidgetAcc* pWidgetAcc =
323       m_pXFADocView->GetWidgetHandler()->GetDataAcc(hWidget);
324 
325   int nRotate = 0;
326 #ifdef PDF_ENABLE_XFA
327   nRotate = pWidgetAcc->GetRotate();
328 #endif
329 
330   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
331   if (pEnv == NULL)
332     return FALSE;
333   FS_RECTF pageViewRect;
334   pEnv->FFI_GetPageViewRect(pPage, pageViewRect);
335 
336   CPDF_Rect rcAnchor;
337 
338   rcAnchor.left = rtAnchor.left;
339   rcAnchor.top = rtAnchor.bottom();
340   rcAnchor.right = rtAnchor.right();
341   rcAnchor.bottom = rtAnchor.top;
342 
343   int t1, t2, t;
344   FX_DWORD dwPos;
345   FX_FLOAT fPoupHeight;
346   switch (nRotate) {
347     case 90: {
348       t1 = (int)(pageViewRect.right - rcAnchor.right);
349       t2 = (int)(rcAnchor.left - pageViewRect.left);
350       if (rcAnchor.bottom < pageViewRect.bottom) {
351         rtPopup.left += rcAnchor.bottom - pageViewRect.bottom;
352       }
353 
354       break;
355     }
356 
357     case 180: {
358       t2 = (int)(pageViewRect.top - rcAnchor.top);
359       t1 = (int)(rcAnchor.bottom - pageViewRect.bottom);
360       if (rcAnchor.left < pageViewRect.left) {
361         rtPopup.left += rcAnchor.left - pageViewRect.left;
362       }
363       break;
364     }
365     case 270: {
366       t1 = (int)(rcAnchor.left - pageViewRect.left);
367       t2 = (int)(pageViewRect.right - rcAnchor.right);
368 
369       if (rcAnchor.top > pageViewRect.top) {
370         rtPopup.left -= rcAnchor.top - pageViewRect.top;
371       }
372       break;
373     }
374     case 0:
375     default: {
376       t1 = (int)(pageViewRect.top - rcAnchor.top);
377       t2 = (int)(rcAnchor.bottom - pageViewRect.bottom);
378       if (rcAnchor.right > pageViewRect.right) {
379         rtPopup.left -= rcAnchor.right - pageViewRect.right;
380       }
381       break;
382     }
383   }
384 
385   if (t1 <= 0 && t2 <= 0) {
386     return FALSE;
387   }
388   if (t1 <= 0) {
389     t = t2;
390     dwPos = 1;
391   } else if (t2 <= 0) {
392     t = t1;
393     dwPos = 0;
394   } else if (t1 > t2) {
395     t = t1;
396     dwPos = 0;
397   } else {
398     t = t2;
399     dwPos = 1;
400   }
401   if (t < fMinPopup) {
402     fPoupHeight = fMinPopup;
403   } else if (t > fMaxPopup) {
404     fPoupHeight = fMaxPopup;
405   } else {
406     fPoupHeight = (FX_FLOAT)t;
407   }
408 
409   switch (nRotate) {
410     case 0:
411     case 180: {
412       if (dwPos == 0) {
413         rtPopup.top = rtAnchor.height;
414         rtPopup.height = fPoupHeight;
415       } else {
416         rtPopup.top = -fPoupHeight;
417         rtPopup.height = fPoupHeight;
418       }
419       break;
420     }
421     case 90:
422     case 270: {
423       if (dwPos == 0) {
424         rtPopup.top = rtAnchor.width;
425         rtPopup.height = fPoupHeight;
426       } else {
427         rtPopup.top = -fPoupHeight;
428         rtPopup.height = fPoupHeight;
429       }
430       break;
431     }
432     default:
433       break;
434   }
435 
436   return TRUE;
437 }
438 
PopupMenu(IXFA_Widget * hWidget,CFX_PointF ptPopup,const CFX_RectF * pRectExclude)439 FX_BOOL CPDFXFA_Document::PopupMenu(IXFA_Widget* hWidget,
440                                     CFX_PointF ptPopup,
441                                     const CFX_RectF* pRectExclude) {
442   if (NULL == hWidget) {
443     return FALSE;
444   }
445   IXFA_PageView* pXFAPageView =
446       m_pXFADocView->GetWidgetHandler()->GetPageView(hWidget);
447   if (pXFAPageView == NULL)
448     return FALSE;
449   CPDFXFA_Page* pPage = GetPage(pXFAPageView);
450 
451   if (pPage == NULL)
452     return FALSE;
453 
454   int menuFlag = 0;
455 
456   IXFA_MenuHandler* pXFAMenuHander = m_pApp->GetXFAApp()->GetMenuHandler();
457   if (pXFAMenuHander->CanUndo(hWidget))
458     menuFlag |= FXFA_MEMU_UNDO;
459   if (pXFAMenuHander->CanRedo(hWidget))
460     menuFlag |= FXFA_MEMU_REDO;
461   if (pXFAMenuHander->CanPaste(hWidget))
462     menuFlag |= FXFA_MEMU_PASTE;
463   if (pXFAMenuHander->CanCopy(hWidget))
464     menuFlag |= FXFA_MEMU_COPY;
465   if (pXFAMenuHander->CanCut(hWidget))
466     menuFlag |= FXFA_MEMU_CUT;
467   if (pXFAMenuHander->CanSelectAll(hWidget))
468     menuFlag |= FXFA_MEMU_SELECTALL;
469 
470   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
471   if (pEnv == NULL)
472     return FALSE;
473 
474   return pEnv->FFI_PopupMenu(pPage, hWidget, menuFlag, ptPopup, NULL);
475 }
476 
PageViewEvent(IXFA_PageView * pPageView,FX_DWORD dwFlags)477 void CPDFXFA_Document::PageViewEvent(IXFA_PageView* pPageView,
478                                      FX_DWORD dwFlags) {
479 }
480 
WidgetEvent(IXFA_Widget * hWidget,CXFA_WidgetAcc * pWidgetData,FX_DWORD dwEvent,void * pParam,void * pAdditional)481 void CPDFXFA_Document::WidgetEvent(IXFA_Widget* hWidget,
482                                    CXFA_WidgetAcc* pWidgetData,
483                                    FX_DWORD dwEvent,
484                                    void* pParam,
485                                    void* pAdditional) {
486   if (m_iDocType != DOCTYPE_DYNAMIC_XFA || !hWidget)
487     return;
488 
489   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
490   if (!pEnv)
491     return;
492 
493   IXFA_PageView* pPageView =
494       m_pXFADocView->GetWidgetHandler()->GetPageView(hWidget);
495   if (pPageView == NULL)
496     return;
497 
498   CPDFXFA_Page* pXFAPage = GetPage(pPageView);
499   if (pXFAPage == NULL)
500     return;
501 
502   CPDFSDK_PageView* pSdkPageView = m_pSDKDoc->GetPageView(pXFAPage);
503   if (dwEvent == XFA_WIDGETEVENT_PostAdded) {
504     pSdkPageView->AddAnnot(hWidget);
505 
506   } else if (dwEvent == XFA_WIDGETEVENT_PreRemoved) {
507     CPDFSDK_Annot* pAnnot = pSdkPageView->GetAnnotByXFAWidget(hWidget);
508     if (pAnnot) {
509       pSdkPageView->DeleteAnnot(pAnnot);
510     }
511   }
512 }
513 
CountPages(IXFA_Doc * hDoc)514 int32_t CPDFXFA_Document::CountPages(IXFA_Doc* hDoc) {
515   if (hDoc == m_pXFADoc && m_pSDKDoc) {
516     return GetPageCount();
517   }
518   return 0;
519 }
GetCurrentPage(IXFA_Doc * hDoc)520 int32_t CPDFXFA_Document::GetCurrentPage(IXFA_Doc* hDoc) {
521   if (hDoc != m_pXFADoc || !m_pSDKDoc)
522     return -1;
523   if (m_iDocType != DOCTYPE_DYNAMIC_XFA)
524     return -1;
525 
526   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
527   if (pEnv == NULL)
528     return -1;
529 
530   return pEnv->FFI_GetCurrentPageIndex(this);
531 }
SetCurrentPage(IXFA_Doc * hDoc,int32_t iCurPage)532 void CPDFXFA_Document::SetCurrentPage(IXFA_Doc* hDoc, int32_t iCurPage) {
533   if (hDoc != m_pXFADoc || !m_pSDKDoc)
534     return;
535   if (m_iDocType != DOCTYPE_DYNAMIC_XFA)
536     return;
537 
538   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
539   if (pEnv == NULL)
540     return;
541 
542   pEnv->FFI_SetCurrentPage(this, iCurPage);
543 }
IsCalculationsEnabled(IXFA_Doc * hDoc)544 FX_BOOL CPDFXFA_Document::IsCalculationsEnabled(IXFA_Doc* hDoc) {
545   if (hDoc != m_pXFADoc || !m_pSDKDoc)
546     return FALSE;
547   if (m_pSDKDoc->GetInterForm())
548     return m_pSDKDoc->GetInterForm()->IsXfaCalculateEnabled();
549 
550   return FALSE;
551 }
SetCalculationsEnabled(IXFA_Doc * hDoc,FX_BOOL bEnabled)552 void CPDFXFA_Document::SetCalculationsEnabled(IXFA_Doc* hDoc,
553                                               FX_BOOL bEnabled) {
554   if (hDoc != m_pXFADoc || !m_pSDKDoc)
555     return;
556   if (m_pSDKDoc->GetInterForm())
557     m_pSDKDoc->GetInterForm()->XfaEnableCalculate(bEnabled);
558 }
559 
GetTitle(IXFA_Doc * hDoc,CFX_WideString & wsTitle)560 void CPDFXFA_Document::GetTitle(IXFA_Doc* hDoc, CFX_WideString& wsTitle) {
561   if (hDoc != m_pXFADoc)
562     return;
563   if (m_pPDFDoc == NULL)
564     return;
565   CPDF_Dictionary* pInfoDict = m_pPDFDoc->GetInfo();
566 
567   if (pInfoDict == NULL)
568     return;
569 
570   CFX_ByteString csTitle = pInfoDict->GetString("Title");
571   wsTitle = wsTitle.FromLocal(csTitle.GetBuffer(csTitle.GetLength()));
572   csTitle.ReleaseBuffer(csTitle.GetLength());
573 }
SetTitle(IXFA_Doc * hDoc,const CFX_WideStringC & wsTitle)574 void CPDFXFA_Document::SetTitle(IXFA_Doc* hDoc,
575                                 const CFX_WideStringC& wsTitle) {
576   if (hDoc != m_pXFADoc)
577     return;
578   if (m_pPDFDoc == NULL)
579     return;
580   CPDF_Dictionary* pInfoDict = m_pPDFDoc->GetInfo();
581 
582   if (pInfoDict == NULL)
583     return;
584   pInfoDict->SetAt("Title", new CPDF_String(wsTitle));
585 }
ExportData(IXFA_Doc * hDoc,const CFX_WideStringC & wsFilePath,FX_BOOL bXDP)586 void CPDFXFA_Document::ExportData(IXFA_Doc* hDoc,
587                                   const CFX_WideStringC& wsFilePath,
588                                   FX_BOOL bXDP) {
589   if (hDoc != m_pXFADoc)
590     return;
591   if (m_iDocType != DOCTYPE_DYNAMIC_XFA && m_iDocType != DOCTYPE_STATIC_XFA)
592     return;
593   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
594   if (pEnv == NULL)
595     return;
596   int fileType = bXDP ? FXFA_SAVEAS_XDP : FXFA_SAVEAS_XML;
597   CFX_ByteString bs = CFX_WideString(wsFilePath).UTF16LE_Encode();
598 
599   if (wsFilePath.IsEmpty()) {
600     if (!pEnv->GetFormFillInfo() ||
601         pEnv->GetFormFillInfo()->m_pJsPlatform == NULL)
602       return;
603     CFX_WideString filepath = pEnv->JS_fieldBrowse();
604     bs = filepath.UTF16LE_Encode();
605   }
606   int len = bs.GetLength() / sizeof(unsigned short);
607   FPDF_FILEHANDLER* pFileHandler = pEnv->FFI_OpenFile(
608       bXDP ? FXFA_SAVEAS_XDP : FXFA_SAVEAS_XML,
609       (FPDF_WIDESTRING)bs.GetBuffer(len * sizeof(unsigned short)), "wb");
610   bs.ReleaseBuffer(len * sizeof(unsigned short));
611 
612   if (pFileHandler == NULL)
613     return;
614 
615   CFPDF_FileStream fileWrite(pFileHandler);
616 
617   IXFA_DocHandler* pXFADocHander = m_pApp->GetXFAApp()->GetDocHandler();
618   CFX_ByteString content;
619   if (fileType == FXFA_SAVEAS_XML) {
620     content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
621     fileWrite.WriteBlock((const FX_CHAR*)content, fileWrite.GetSize(),
622                          content.GetLength());
623     CFX_WideStringC data(L"data");
624     if (pXFADocHander->SavePackage(m_pXFADocView->GetDoc(), data, &fileWrite)) {
625       // TODO: Maybe report error.
626     }
627   } else if (fileType == FXFA_SAVEAS_XDP) {
628     if (m_pPDFDoc == NULL)
629       return;
630     CPDF_Dictionary* pRoot = m_pPDFDoc->GetRoot();
631     if (pRoot == NULL)
632       return;
633     CPDF_Dictionary* pAcroForm = pRoot->GetDict("AcroForm");
634     if (NULL == pAcroForm)
635       return;
636     CPDF_Object* pXFA = pAcroForm->GetElement("XFA");
637     if (pXFA == NULL)
638       return;
639     if (pXFA->GetType() != PDFOBJ_ARRAY)
640       return;
641     CPDF_Array* pArray = pXFA->GetArray();
642     if (NULL == pArray)
643       return;
644     int size = pArray->GetCount();
645     for (int i = 1; i < size; i += 2) {
646       CPDF_Object* pPDFObj = pArray->GetElement(i);
647       CPDF_Object* pPrePDFObj = pArray->GetElement(i - 1);
648       if (pPrePDFObj->GetType() != PDFOBJ_STRING)
649         continue;
650       if (pPDFObj->GetType() != PDFOBJ_REFERENCE)
651         continue;
652       CPDF_Object* pDirectObj = pPDFObj->GetDirect();
653       if (pDirectObj->GetType() != PDFOBJ_STREAM)
654         continue;
655       if (pPrePDFObj->GetString() == "form") {
656         CFX_WideStringC form(L"form");
657         pXFADocHander->SavePackage(m_pXFADocView->GetDoc(), form, &fileWrite);
658       } else if (pPrePDFObj->GetString() == "datasets") {
659         CFX_WideStringC datasets(L"datasets");
660         pXFADocHander->SavePackage(m_pXFADocView->GetDoc(), datasets,
661                                    &fileWrite);
662       } else {
663         if (i == size - 1) {
664           CFX_WideString wPath = CFX_WideString::FromUTF16LE(
665               (unsigned short*)(const FX_CHAR*)bs,
666               bs.GetLength() / sizeof(unsigned short));
667           CFX_ByteString bPath = wPath.UTF8Encode();
668           CFX_ByteString szFormat =
669               "\n<pdf href=\"%s\" xmlns=\"http://ns.adobe.com/xdp/pdf/\"/>";
670           content.Format(szFormat, (char*)(const FX_CHAR*)bPath);
671           fileWrite.WriteBlock((const FX_CHAR*)content, fileWrite.GetSize(),
672                                content.GetLength());
673         }
674 
675         CPDF_Stream* pStream = (CPDF_Stream*)pDirectObj;
676         CPDF_StreamAcc* pAcc = new CPDF_StreamAcc;
677         pAcc->LoadAllData(pStream);
678         fileWrite.WriteBlock(pAcc->GetData(), fileWrite.GetSize(),
679                              pAcc->GetSize());
680         delete pAcc;
681       }
682     }
683   }
684   if (!fileWrite.Flush()) {
685     // TODO: Report error.
686   }
687 }
ImportData(IXFA_Doc * hDoc,const CFX_WideStringC & wsFilePath)688 void CPDFXFA_Document::ImportData(IXFA_Doc* hDoc,
689                                   const CFX_WideStringC& wsFilePath) {
690   // TODO...
691 }
692 
GotoURL(IXFA_Doc * hDoc,const CFX_WideStringC & bsURL,FX_BOOL bAppend)693 void CPDFXFA_Document::GotoURL(IXFA_Doc* hDoc,
694                                const CFX_WideStringC& bsURL,
695                                FX_BOOL bAppend) {
696   if (hDoc != m_pXFADoc)
697     return;
698 
699   if (m_iDocType != DOCTYPE_DYNAMIC_XFA)
700     return;
701 
702   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
703   if (pEnv == NULL)
704     return;
705 
706   CFX_WideStringC str(bsURL.GetPtr());
707 
708   pEnv->FFI_GotoURL(this, str, bAppend);
709 }
710 
IsValidationsEnabled(IXFA_Doc * hDoc)711 FX_BOOL CPDFXFA_Document::IsValidationsEnabled(IXFA_Doc* hDoc) {
712   if (hDoc != m_pXFADoc || !m_pSDKDoc)
713     return FALSE;
714   if (m_pSDKDoc->GetInterForm())
715     return m_pSDKDoc->GetInterForm()->IsXfaValidationsEnabled();
716 
717   return TRUE;
718 }
SetValidationsEnabled(IXFA_Doc * hDoc,FX_BOOL bEnabled)719 void CPDFXFA_Document::SetValidationsEnabled(IXFA_Doc* hDoc, FX_BOOL bEnabled) {
720   if (hDoc != m_pXFADoc || !m_pSDKDoc)
721     return;
722   if (m_pSDKDoc->GetInterForm())
723     m_pSDKDoc->GetInterForm()->XfaSetValidationsEnabled(bEnabled);
724 }
SetFocusWidget(IXFA_Doc * hDoc,IXFA_Widget * hWidget)725 void CPDFXFA_Document::SetFocusWidget(IXFA_Doc* hDoc, IXFA_Widget* hWidget) {
726   if (hDoc != m_pXFADoc)
727     return;
728 
729   if (NULL == hWidget) {
730     m_pSDKDoc->SetFocusAnnot(NULL);
731     return;
732   }
733 
734   int pageViewCount = m_pSDKDoc->GetPageViewCount();
735   for (int i = 0; i < pageViewCount; i++) {
736     CPDFSDK_PageView* pPageView = m_pSDKDoc->GetPageView(i);
737     if (pPageView == NULL)
738       continue;
739     CPDFSDK_Annot* pAnnot = pPageView->GetAnnotByXFAWidget(hWidget);
740     if (pAnnot) {
741       m_pSDKDoc->SetFocusAnnot(pAnnot);
742       break;
743     }
744   }
745 }
Print(IXFA_Doc * hDoc,int32_t nStartPage,int32_t nEndPage,FX_DWORD dwOptions)746 void CPDFXFA_Document::Print(IXFA_Doc* hDoc,
747                              int32_t nStartPage,
748                              int32_t nEndPage,
749                              FX_DWORD dwOptions) {
750   if (hDoc != m_pXFADoc)
751     return;
752 
753   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
754   if (pEnv == NULL)
755     return;
756 
757   if (!pEnv->GetFormFillInfo() ||
758       pEnv->GetFormFillInfo()->m_pJsPlatform == NULL)
759     return;
760   if (pEnv->GetFormFillInfo()->m_pJsPlatform->Doc_print == NULL)
761     return;
762   pEnv->GetFormFillInfo()->m_pJsPlatform->Doc_print(
763       pEnv->GetFormFillInfo()->m_pJsPlatform,
764       dwOptions & XFA_PRINTOPT_ShowDialog, nStartPage, nEndPage,
765       dwOptions & XFA_PRINTOPT_CanCancel, dwOptions & XFA_PRINTOPT_ShrinkPage,
766       dwOptions & XFA_PRINTOPT_AsImage, dwOptions & XFA_PRINTOPT_ReverseOrder,
767       dwOptions & XFA_PRINTOPT_PrintAnnot);
768 }
769 
GetURL(IXFA_Doc * hDoc,CFX_WideString & wsDocURL)770 void CPDFXFA_Document::GetURL(IXFA_Doc* hDoc, CFX_WideString& wsDocURL) {
771   if (hDoc != m_pXFADoc)
772     return;
773 
774   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
775   if (pEnv == NULL)
776     return;
777 
778   pEnv->FFI_GetURL(this, wsDocURL);
779 }
780 
GetHighlightColor(IXFA_Doc * hDoc)781 FX_ARGB CPDFXFA_Document::GetHighlightColor(IXFA_Doc* hDoc) {
782   if (hDoc != m_pXFADoc)
783     return 0;
784   if (m_pSDKDoc) {
785     if (CPDFSDK_InterForm* pInterForm = m_pSDKDoc->GetInterForm()) {
786       FX_COLORREF color = pInterForm->GetHighlightColor(FPDF_FORMFIELD_XFA);
787       uint8_t alpha = pInterForm->GetHighlightAlpha();
788       FX_ARGB argb = ArgbEncode((int)alpha, color);
789       return argb;
790     }
791   }
792   return 0;
793 }
794 
AddDoRecord(IXFA_Widget * hWidget)795 void CPDFXFA_Document::AddDoRecord(IXFA_Widget* hWidget) {
796   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
797   if (pEnv == NULL)
798     return;
799   return;
800   // pEnv->FFI_AddDoRecord(this, hWidget);
801 }
802 
_NotifySubmit(FX_BOOL bPrevOrPost)803 FX_BOOL CPDFXFA_Document::_NotifySubmit(FX_BOOL bPrevOrPost) {
804   if (bPrevOrPost)
805     return _OnBeforeNotifySumbit();
806   else
807     _OnAfterNotifySumbit();
808   return TRUE;
809 }
810 
_OnBeforeNotifySumbit()811 FX_BOOL CPDFXFA_Document::_OnBeforeNotifySumbit() {
812 #ifdef PDF_ENABLE_XFA
813   if (m_iDocType != DOCTYPE_DYNAMIC_XFA && m_iDocType != DOCTYPE_STATIC_XFA)
814     return TRUE;
815   if (m_pXFADocView == NULL)
816     return TRUE;
817   IXFA_WidgetHandler* pWidgetHandler = m_pXFADocView->GetWidgetHandler();
818   if (pWidgetHandler == NULL)
819     return TRUE;
820   IXFA_WidgetAccIterator* pWidgetAccIterator =
821       m_pXFADocView->CreateWidgetAccIterator();
822   if (pWidgetAccIterator) {
823     CXFA_EventParam Param;
824     Param.m_eType = XFA_EVENT_PreSubmit;
825     CXFA_WidgetAcc* pWidgetAcc = pWidgetAccIterator->MoveToNext();
826     while (pWidgetAcc) {
827       pWidgetHandler->ProcessEvent(pWidgetAcc, &Param);
828       pWidgetAcc = pWidgetAccIterator->MoveToNext();
829     }
830     pWidgetAccIterator->Release();
831   }
832   pWidgetAccIterator = m_pXFADocView->CreateWidgetAccIterator();
833   if (pWidgetAccIterator) {
834     CXFA_WidgetAcc* pWidgetAcc = pWidgetAccIterator->MoveToNext();
835     pWidgetAcc = pWidgetAccIterator->MoveToNext();
836     while (pWidgetAcc) {
837       int fRet = pWidgetAcc->ProcessValidate(-1);
838       if (fRet == XFA_EVENTERROR_Error) {
839         CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
840         if (pEnv == NULL)
841           return FALSE;
842         CFX_WideString ws;
843         ws.FromLocal(IDS_XFA_Validate_Input);
844         CFX_ByteString bs = ws.UTF16LE_Encode();
845         int len = bs.GetLength() / sizeof(unsigned short);
846         pEnv->FFI_Alert(
847             (FPDF_WIDESTRING)bs.GetBuffer(len * sizeof(unsigned short)),
848             (FPDF_WIDESTRING)L"", 0, 1);
849         bs.ReleaseBuffer(len * sizeof(unsigned short));
850         pWidgetAccIterator->Release();
851         return FALSE;
852       }
853       pWidgetAcc = pWidgetAccIterator->MoveToNext();
854     }
855     pWidgetAccIterator->Release();
856     m_pXFADocView->UpdateDocView();
857   }
858 #endif
859   return TRUE;
860 }
_OnAfterNotifySumbit()861 void CPDFXFA_Document::_OnAfterNotifySumbit() {
862   if (m_iDocType != DOCTYPE_DYNAMIC_XFA && m_iDocType != DOCTYPE_STATIC_XFA)
863     return;
864   if (m_pXFADocView == NULL)
865     return;
866   IXFA_WidgetHandler* pWidgetHandler = m_pXFADocView->GetWidgetHandler();
867   if (pWidgetHandler == NULL)
868     return;
869   IXFA_WidgetAccIterator* pWidgetAccIterator =
870       m_pXFADocView->CreateWidgetAccIterator();
871   if (pWidgetAccIterator == NULL)
872     return;
873   CXFA_EventParam Param;
874   Param.m_eType = XFA_EVENT_PostSubmit;
875 
876   CXFA_WidgetAcc* pWidgetAcc = pWidgetAccIterator->MoveToNext();
877   while (pWidgetAcc) {
878     pWidgetHandler->ProcessEvent(pWidgetAcc, &Param);
879     pWidgetAcc = pWidgetAccIterator->MoveToNext();
880   }
881   pWidgetAccIterator->Release();
882   m_pXFADocView->UpdateDocView();
883 }
884 
SubmitData(IXFA_Doc * hDoc,CXFA_Submit submit)885 FX_BOOL CPDFXFA_Document::SubmitData(IXFA_Doc* hDoc, CXFA_Submit submit) {
886   if (!_NotifySubmit(TRUE))
887     return FALSE;
888   if (NULL == m_pXFADocView)
889     return FALSE;
890   m_pXFADocView->UpdateDocView();
891 
892   FX_BOOL ret = _SubmitData(hDoc, submit);
893   _NotifySubmit(FALSE);
894   return ret;
895 }
896 
OpenLinkedFile(IXFA_Doc * hDoc,const CFX_WideString & wsLink)897 IFX_FileRead* CPDFXFA_Document::OpenLinkedFile(IXFA_Doc* hDoc,
898                                                const CFX_WideString& wsLink) {
899   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
900   if (pEnv == NULL)
901     return FALSE;
902   CFX_ByteString bs = wsLink.UTF16LE_Encode();
903   int len = bs.GetLength() / sizeof(unsigned short);
904   FPDF_FILEHANDLER* pFileHandler = pEnv->FFI_OpenFile(
905       0, (FPDF_WIDESTRING)bs.GetBuffer(len * sizeof(unsigned short)), "rb");
906   bs.ReleaseBuffer(len * sizeof(unsigned short));
907 
908   if (pFileHandler == NULL)
909     return NULL;
910   return new CFPDF_FileStream(pFileHandler);
911 }
_ExportSubmitFile(FPDF_FILEHANDLER * pFileHandler,int fileType,FPDF_DWORD encodeType,FPDF_DWORD flag)912 FX_BOOL CPDFXFA_Document::_ExportSubmitFile(FPDF_FILEHANDLER* pFileHandler,
913                                             int fileType,
914                                             FPDF_DWORD encodeType,
915                                             FPDF_DWORD flag) {
916   if (NULL == m_pXFADocView)
917     return FALSE;
918   IXFA_DocHandler* pDocHandler = m_pApp->GetXFAApp()->GetDocHandler();
919   CFX_ByteString content;
920 
921   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
922   if (pEnv == NULL)
923     return FALSE;
924 
925   CFPDF_FileStream fileStream(pFileHandler);
926 
927   if (fileType == FXFA_SAVEAS_XML) {
928     CFX_WideString ws;
929     ws.FromLocal("data");
930     CFX_ByteString content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
931     fileStream.WriteBlock((const FX_CHAR*)content, 0, content.GetLength());
932     pDocHandler->SavePackage(m_pXFADoc, ws, &fileStream);
933   } else if (fileType == FXFA_SAVEAS_XDP) {
934     if (flag == 0)
935       flag = FXFA_CONFIG | FXFA_TEMPLATE | FXFA_LOCALESET | FXFA_DATASETS |
936              FXFA_XMPMETA | FXFA_XFDF | FXFA_FORM;
937     if (m_pPDFDoc == NULL) {
938       fileStream.Flush();
939       return FALSE;
940     }
941     CPDF_Dictionary* pRoot = m_pPDFDoc->GetRoot();
942     if (pRoot == NULL) {
943       fileStream.Flush();
944       return FALSE;
945     }
946     CPDF_Dictionary* pAcroForm = pRoot->GetDict("AcroForm");
947     if (NULL == pAcroForm) {
948       fileStream.Flush();
949       return FALSE;
950     }
951     CPDF_Object* pXFA = pAcroForm->GetElement("XFA");
952     if (pXFA == NULL) {
953       fileStream.Flush();
954       return FALSE;
955     }
956     if (pXFA->GetType() != PDFOBJ_ARRAY) {
957       fileStream.Flush();
958       return FALSE;
959     }
960     CPDF_Array* pArray = pXFA->GetArray();
961     if (NULL == pArray) {
962       fileStream.Flush();
963       return FALSE;
964     }
965     int size = pArray->GetCount();
966     for (int i = 1; i < size; i += 2) {
967       CPDF_Object* pPDFObj = pArray->GetElement(i);
968       CPDF_Object* pPrePDFObj = pArray->GetElement(i - 1);
969       if (pPrePDFObj->GetType() != PDFOBJ_STRING)
970         continue;
971       if (pPDFObj->GetType() != PDFOBJ_REFERENCE)
972         continue;
973       CPDF_Object* pDirectObj = pPDFObj->GetDirect();
974       if (pDirectObj->GetType() != PDFOBJ_STREAM)
975         continue;
976       if (pPrePDFObj->GetString() == "config" && !(flag & FXFA_CONFIG))
977         continue;
978       if (pPrePDFObj->GetString() == "template" && !(flag & FXFA_TEMPLATE))
979         continue;
980       if (pPrePDFObj->GetString() == "localeSet" && !(flag & FXFA_LOCALESET))
981         continue;
982       if (pPrePDFObj->GetString() == "datasets" && !(flag & FXFA_DATASETS))
983         continue;
984       if (pPrePDFObj->GetString() == "xmpmeta" && !(flag & FXFA_XMPMETA))
985         continue;
986       if (pPrePDFObj->GetString() == "xfdf" && !(flag & FXFA_XFDF))
987         continue;
988       if (pPrePDFObj->GetString() == "form" && !(flag & FXFA_FORM))
989         continue;
990       if (pPrePDFObj->GetString() == "form") {
991         CFX_WideString ws;
992         ws.FromLocal("form");
993         pDocHandler->SavePackage(m_pXFADoc, ws, &fileStream);
994       } else if (pPrePDFObj->GetString() == "datasets") {
995         CFX_WideString ws;
996         ws.FromLocal("datasets");
997         pDocHandler->SavePackage(m_pXFADoc, ws, &fileStream);
998       } else {
999         // PDF,creator.
1000         // TODO:
1001       }
1002     }
1003   }
1004   return TRUE;
1005 }
1006 
_ClearChangeMark()1007 void CPDFXFA_Document::_ClearChangeMark() {
1008   if (m_pSDKDoc)
1009     m_pSDKDoc->ClearChangeMark();
1010 }
1011 
_ToXFAContentFlags(CFX_WideString csSrcContent,FPDF_DWORD & flag)1012 void CPDFXFA_Document::_ToXFAContentFlags(CFX_WideString csSrcContent,
1013                                           FPDF_DWORD& flag) {
1014   if (csSrcContent.Find(L" config ", 0) != -1)
1015     flag |= FXFA_CONFIG;
1016   if (csSrcContent.Find(L" template ", 0) != -1)
1017     flag |= FXFA_TEMPLATE;
1018   if (csSrcContent.Find(L" localeSet ", 0) != -1)
1019     flag |= FXFA_LOCALESET;
1020   if (csSrcContent.Find(L" datasets ", 0) != -1)
1021     flag |= FXFA_DATASETS;
1022   if (csSrcContent.Find(L" xmpmeta ", 0) != -1)
1023     flag |= FXFA_XMPMETA;
1024   if (csSrcContent.Find(L" xfdf ", 0) != -1)
1025     flag |= FXFA_XFDF;
1026   if (csSrcContent.Find(L" form ", 0) != -1)
1027     flag |= FXFA_FORM;
1028   if (flag == 0)
1029     flag = FXFA_CONFIG | FXFA_TEMPLATE | FXFA_LOCALESET | FXFA_DATASETS |
1030            FXFA_XMPMETA | FXFA_XFDF | FXFA_FORM;
1031 }
_MailToInfo(CFX_WideString & csURL,CFX_WideString & csToAddress,CFX_WideString & csCCAddress,CFX_WideString & csBCCAddress,CFX_WideString & csSubject,CFX_WideString & csMsg)1032 FX_BOOL CPDFXFA_Document::_MailToInfo(CFX_WideString& csURL,
1033                                       CFX_WideString& csToAddress,
1034                                       CFX_WideString& csCCAddress,
1035                                       CFX_WideString& csBCCAddress,
1036                                       CFX_WideString& csSubject,
1037                                       CFX_WideString& csMsg) {
1038   CFX_WideString srcURL = csURL;
1039   srcURL.TrimLeft();
1040   if (0 != srcURL.Left(7).CompareNoCase(L"mailto:"))
1041     return FALSE;
1042   int pos = srcURL.Find(L'?', 0);
1043   CFX_WideString tmp;
1044   if (pos == -1) {
1045     pos = srcURL.Find(L'@', 0);
1046     if (pos == -1)
1047       return FALSE;
1048     else {
1049       tmp = srcURL.Right(csURL.GetLength() - 7);
1050       tmp.TrimLeft();
1051       tmp.TrimRight();
1052     }
1053   } else {
1054     tmp = srcURL.Left(pos);
1055     tmp = tmp.Right(tmp.GetLength() - 7);
1056     tmp.TrimLeft();
1057     tmp.TrimRight();
1058   }
1059 
1060   csToAddress = tmp;
1061 
1062   srcURL = srcURL.Right(srcURL.GetLength() - (pos + 1));
1063   while (!srcURL.IsEmpty()) {
1064     srcURL.TrimLeft();
1065     srcURL.TrimRight();
1066     pos = srcURL.Find(L'&', 0);
1067     if (pos == -1)
1068       tmp = srcURL;
1069     else
1070       tmp = srcURL.Left(pos);
1071 
1072     tmp.TrimLeft();
1073     tmp.TrimRight();
1074     if (tmp.GetLength() >= 3 && 0 == tmp.Left(3).CompareNoCase(L"cc=")) {
1075       tmp = tmp.Right(tmp.GetLength() - 3);
1076       if (!csCCAddress.IsEmpty())
1077         csCCAddress += L';';
1078       csCCAddress += tmp;
1079 
1080     } else if (tmp.GetLength() >= 4 &&
1081                0 == tmp.Left(4).CompareNoCase(L"bcc=")) {
1082       tmp = tmp.Right(tmp.GetLength() - 4);
1083       if (!csBCCAddress.IsEmpty())
1084         csBCCAddress += L';';
1085       csBCCAddress += tmp;
1086     } else if (tmp.GetLength() >= 8 &&
1087                0 == tmp.Left(8).CompareNoCase(L"subject=")) {
1088       tmp = tmp.Right(tmp.GetLength() - 8);
1089       csSubject += tmp;
1090     } else if (tmp.GetLength() >= 5 &&
1091                0 == tmp.Left(5).CompareNoCase(L"body=")) {
1092       tmp = tmp.Right(tmp.GetLength() - 5);
1093       csMsg += tmp;
1094     }
1095     if (pos == -1)
1096       srcURL = L"";
1097     else
1098       srcURL = srcURL.Right(csURL.GetLength() - (pos + 1));
1099   }
1100   csToAddress.Replace(L",", L";");
1101   csCCAddress.Replace(L",", L";");
1102   csBCCAddress.Replace(L",", L";");
1103   return TRUE;
1104 }
1105 
_SubmitData(IXFA_Doc * hDoc,CXFA_Submit submit)1106 FX_BOOL CPDFXFA_Document::_SubmitData(IXFA_Doc* hDoc, CXFA_Submit submit) {
1107 #ifdef PDF_ENABLE_XFA
1108   CFX_WideStringC csURLC;
1109   submit.GetSubmitTarget(csURLC);
1110   CFX_WideString csURL = csURLC;
1111   CPDFDoc_Environment* pEnv = m_pSDKDoc->GetEnv();
1112   if (pEnv == NULL)
1113     return FALSE;
1114   if (csURL.IsEmpty()) {
1115     CFX_WideString ws;
1116     ws.FromLocal("Submit cancelled.");
1117     CFX_ByteString bs = ws.UTF16LE_Encode();
1118     int len = bs.GetLength() / sizeof(unsigned short);
1119     pEnv->FFI_Alert((FPDF_WIDESTRING)bs.GetBuffer(len * sizeof(unsigned short)),
1120                     (FPDF_WIDESTRING)L"", 0, 4);
1121     bs.ReleaseBuffer(len * sizeof(unsigned short));
1122     return FALSE;
1123   }
1124 
1125   FPDF_BOOL bRet = TRUE;
1126   FPDF_FILEHANDLER* pFileHandler = NULL;
1127   int fileFlag = -1;
1128 
1129   if (submit.GetSubmitFormat() == XFA_ATTRIBUTEENUM_Xdp) {
1130     CFX_WideStringC csContentC;
1131     submit.GetSubmitXDPContent(csContentC);
1132     CFX_WideString csContent;
1133     csContent = csContentC.GetPtr();
1134     csContent.TrimLeft();
1135     csContent.TrimRight();
1136     CFX_WideString space;
1137     space.FromLocal(" ");
1138     csContent = space + csContent + space;
1139     FPDF_DWORD flag = 0;
1140     if (submit.IsSubmitEmbedPDF())
1141       flag |= FXFA_PDF;
1142     _ToXFAContentFlags(csContent, flag);
1143     pFileHandler = pEnv->FFI_OpenFile(FXFA_SAVEAS_XDP, NULL, "wb");
1144     fileFlag = FXFA_SAVEAS_XDP;
1145     _ExportSubmitFile(pFileHandler, FXFA_SAVEAS_XDP, 0, flag);
1146   } else if (submit.GetSubmitFormat() == XFA_ATTRIBUTEENUM_Xml) {
1147     pFileHandler = pEnv->FFI_OpenFile(FXFA_SAVEAS_XML, NULL, "wb");
1148     fileFlag = FXFA_SAVEAS_XML;
1149     _ExportSubmitFile(pFileHandler, FXFA_SAVEAS_XML, 0);
1150   } else if (submit.GetSubmitFormat() == XFA_ATTRIBUTEENUM_Pdf) {
1151     // csfilename = csDocName;
1152   } else if (submit.GetSubmitFormat() == XFA_ATTRIBUTEENUM_Formdata) {
1153     return FALSE;
1154   } else if (submit.GetSubmitFormat() == XFA_ATTRIBUTEENUM_Urlencoded) {
1155     pFileHandler = pEnv->FFI_OpenFile(FXFA_SAVEAS_XML, NULL, "wb");
1156     fileFlag = FXFA_SAVEAS_XML;
1157     _ExportSubmitFile(pFileHandler, FXFA_SAVEAS_XML, 0);
1158   } else if (submit.GetSubmitFormat() == XFA_ATTRIBUTEENUM_Xfd) {
1159     return FALSE;
1160   } else {
1161     return FALSE;
1162   }
1163   if (pFileHandler == NULL)
1164     return FALSE;
1165   if (0 == csURL.Left(7).CompareNoCase(L"mailto:")) {
1166     CFX_WideString csToAddress;
1167     CFX_WideString csCCAddress;
1168     CFX_WideString csBCCAddress;
1169     CFX_WideString csSubject;
1170     CFX_WideString csMsg;
1171 
1172     bRet = _MailToInfo(csURL, csToAddress, csCCAddress, csBCCAddress, csSubject,
1173                        csMsg);
1174     if (FALSE == bRet)
1175       return FALSE;
1176 
1177     CFX_ByteString bsTo = CFX_WideString(csToAddress).UTF16LE_Encode();
1178     CFX_ByteString bsCC = CFX_WideString(csCCAddress).UTF16LE_Encode();
1179     CFX_ByteString bsBcc = CFX_WideString(csBCCAddress).UTF16LE_Encode();
1180     CFX_ByteString bsSubject = CFX_WideString(csSubject).UTF16LE_Encode();
1181     CFX_ByteString bsMsg = CFX_WideString(csMsg).UTF16LE_Encode();
1182 
1183     FPDF_WIDESTRING pTo = (FPDF_WIDESTRING)bsTo.GetBuffer(bsTo.GetLength());
1184     FPDF_WIDESTRING pCC = (FPDF_WIDESTRING)bsCC.GetBuffer(bsCC.GetLength());
1185     FPDF_WIDESTRING pBcc = (FPDF_WIDESTRING)bsBcc.GetBuffer(bsBcc.GetLength());
1186     FPDF_WIDESTRING pSubject =
1187         (FPDF_WIDESTRING)bsSubject.GetBuffer(bsSubject.GetLength());
1188     FPDF_WIDESTRING pMsg = (FPDF_WIDESTRING)bsMsg.GetBuffer(bsMsg.GetLength());
1189 
1190     pEnv->FFI_EmailTo(pFileHandler, pTo, pSubject, pCC, pBcc, pMsg);
1191     bsTo.ReleaseBuffer();
1192     bsCC.ReleaseBuffer();
1193     bsBcc.ReleaseBuffer();
1194     bsSubject.ReleaseBuffer();
1195     bsMsg.ReleaseBuffer();
1196   } else {
1197     // http��ftp
1198     CFX_WideString ws;
1199     CFX_ByteString bs = csURL.UTF16LE_Encode();
1200     int len = bs.GetLength() / sizeof(unsigned short);
1201     pEnv->FFI_UploadTo(
1202         pFileHandler, fileFlag,
1203         (FPDF_WIDESTRING)bs.GetBuffer(len * sizeof(unsigned short)));
1204     bs.ReleaseBuffer(len * sizeof(unsigned short));
1205   }
1206 
1207   return bRet;
1208 #else
1209   return TRUE;
1210 #endif
1211 }
1212 
SetGlobalProperty(IXFA_Doc * hDoc,const CFX_ByteStringC & szPropName,FXJSE_HVALUE hValue)1213 FX_BOOL CPDFXFA_Document::SetGlobalProperty(IXFA_Doc* hDoc,
1214                                             const CFX_ByteStringC& szPropName,
1215                                             FXJSE_HVALUE hValue) {
1216   if (hDoc != m_pXFADoc)
1217     return FALSE;
1218 
1219   if (m_pSDKDoc && m_pSDKDoc->GetEnv()->GetJSRuntime())
1220     return m_pSDKDoc->GetEnv()->GetJSRuntime()->SetHValueByName(szPropName,
1221                                                                 hValue);
1222   return FALSE;
1223 }
GetPDFScriptObject(IXFA_Doc * hDoc,const CFX_ByteStringC & utf8Name,FXJSE_HVALUE hValue)1224 FX_BOOL CPDFXFA_Document::GetPDFScriptObject(IXFA_Doc* hDoc,
1225                                              const CFX_ByteStringC& utf8Name,
1226                                              FXJSE_HVALUE hValue) {
1227   if (hDoc != m_pXFADoc)
1228     return FALSE;
1229 
1230   if (!m_pSDKDoc || !m_pSDKDoc->GetEnv()->GetJSRuntime())
1231     return FALSE;
1232 
1233   if (!m_pJSContext) {
1234     m_pSDKDoc->GetEnv()->GetJSRuntime()->SetReaderDocument(m_pSDKDoc);
1235     m_pJSContext = m_pSDKDoc->GetEnv()->GetJSRuntime()->NewContext();
1236   }
1237 
1238   return _GetHValueByName(utf8Name, hValue,
1239                           m_pSDKDoc->GetEnv()->GetJSRuntime());
1240 }
GetGlobalProperty(IXFA_Doc * hDoc,const CFX_ByteStringC & szPropName,FXJSE_HVALUE hValue)1241 FX_BOOL CPDFXFA_Document::GetGlobalProperty(IXFA_Doc* hDoc,
1242                                             const CFX_ByteStringC& szPropName,
1243                                             FXJSE_HVALUE hValue) {
1244   if (hDoc != m_pXFADoc)
1245     return FALSE;
1246   if (!m_pSDKDoc || !m_pSDKDoc->GetEnv()->GetJSRuntime())
1247     return FALSE;
1248 
1249   if (!m_pJSContext) {
1250     m_pSDKDoc->GetEnv()->GetJSRuntime()->SetReaderDocument(m_pSDKDoc);
1251     m_pJSContext = m_pSDKDoc->GetEnv()->GetJSRuntime()->NewContext();
1252   }
1253 
1254   return _GetHValueByName(szPropName, hValue,
1255                           m_pSDKDoc->GetEnv()->GetJSRuntime());
1256 }
_GetHValueByName(const CFX_ByteStringC & utf8Name,FXJSE_HVALUE hValue,IJS_Runtime * runTime)1257 FX_BOOL CPDFXFA_Document::_GetHValueByName(const CFX_ByteStringC& utf8Name,
1258                                            FXJSE_HVALUE hValue,
1259                                            IJS_Runtime* runTime) {
1260   return runTime->GetHValueByName(utf8Name, hValue);
1261 }
1262