1 /* AbiWord
2  * Copyright (C) 2003 Dom Lachowicz
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA.
18  */
19 
20 #include "xap_App.h"
21 #include "xap_Dialog_Id.h"
22 #include "xap_DialogFactory.h"
23 #include "ap_Dialog_MailMerge.h"
24 #include "xap_Dlg_FileOpenSaveAs.h"
25 #include "ie_mailmerge.h"
26 #include "xap_Frame.h"
27 #include "fv_View.h"
28 #include "ut_assert.h"
29 #include "pd_Document.h"
30 
AP_Dialog_MailMerge(XAP_DialogFactory * pDlgFactory,XAP_Dialog_Id id)31 AP_Dialog_MailMerge::AP_Dialog_MailMerge(XAP_DialogFactory * pDlgFactory,
32 					   XAP_Dialog_Id id)
33 	: XAP_Dialog_Modeless(pDlgFactory,id, "interface/dialogmailmerge"),  m_pFrame(0)
34 {
35 }
36 
~AP_Dialog_MailMerge(void)37 AP_Dialog_MailMerge::~AP_Dialog_MailMerge(void)
38 {
39 	UT_VECTOR_PURGEALL(UT_UTF8String*, m_vecFields);
40 }
41 
setMergeField(const UT_UTF8String & name)42 void AP_Dialog_MailMerge::setMergeField(const UT_UTF8String & name)
43 {
44 	m_mergeField = name;
45 }
46 
getMergeField() const47 const UT_UTF8String& AP_Dialog_MailMerge::getMergeField() const
48 {
49 	return m_mergeField;
50 }
51 
init()52 void AP_Dialog_MailMerge::init ()
53 {
54 	UT_return_if_fail(m_pFrame);
55 
56 	PD_Document * pDoc = static_cast<PD_Document*>(m_pFrame->getCurrentDoc());
57 	UT_UTF8String link (pDoc->getMailMergeLink());
58 
59 	if (link.size()) {
60 
61 		IE_MailMerge * pie = NULL;
62 		UT_Error errorCode = IE_MailMerge::constructMerger(link.utf8_str(), IEMT_Unknown, &pie);
63 		if (!errorCode && pie)
64 		{
65 			pie->getHeaders (link.utf8_str(), m_vecFields);
66 			DELETEP(pie);
67 			setFieldList();
68 		}
69 	}
70 }
71 
eventOpen()72 void AP_Dialog_MailMerge::eventOpen ()
73 {
74 	UT_VECTOR_PURGEALL(UT_UTF8String*, m_vecFields);
75 	m_vecFields.clear();
76 
77 	UT_return_if_fail(m_pFrame);
78 	m_pFrame->raise();
79 	XAP_Dialog_Id id = XAP_DIALOG_ID_FILE_OPEN;
80 
81 	XAP_DialogFactory * pDialogFactory
82 		= static_cast<XAP_DialogFactory *>(m_pFrame->getDialogFactory());
83 
84 	XAP_Dialog_FileOpenSaveAs * pDialog
85 		= static_cast<XAP_Dialog_FileOpenSaveAs *>(pDialogFactory->requestDialog(id));
86 	UT_return_if_fail (pDialog);
87 
88 	UT_uint32 filterCount = 0;
89 
90 	filterCount = IE_MailMerge::getMergerCount();
91 
92 	const char ** szDescList = static_cast<const char **>(UT_calloc(filterCount + 1, sizeof(char *)));
93 	const char ** szSuffixList = static_cast<const char **>(UT_calloc(filterCount + 1, sizeof(char *)));
94 	IEMergeType * nTypeList = static_cast<IEMergeType *>(UT_calloc(filterCount + 1, sizeof(IEMergeType)));
95 	UT_uint32 k = 0;
96 
97 	while (IE_MailMerge::enumerateDlgLabels(k, &szDescList[k], &szSuffixList[k], &nTypeList[k]))
98 		k++;
99 
100 	pDialog->setFileTypeList(szDescList, szSuffixList, static_cast<const UT_sint32 *>(nTypeList));
101 
102 	pDialog->setDefaultFileType(IE_MailMerge::fileTypeForSuffix (".xml"));
103 
104 	pDialog->runModal(m_pFrame);
105 
106 	XAP_Dialog_FileOpenSaveAs::tAnswer ans = pDialog->getAnswer();
107 	bool bOK = (ans == XAP_Dialog_FileOpenSaveAs::a_OK);
108 
109 	if (bOK)
110     {
111 		UT_UTF8String filename (pDialog->getPathname());
112 		UT_sint32 type = pDialog->getFileType();
113 
114 		IE_MailMerge * pie = NULL;
115 		UT_Error errorCode = IE_MailMerge::constructMerger(filename.utf8_str(), static_cast<IEMergeType>(type), &pie);
116 		if (!errorCode && pie)
117 		{
118 			pie->getHeaders (filename.utf8_str(), m_vecFields);
119 			DELETEP(pie);
120 		}
121 	}
122 
123 	pDialogFactory->releaseDialog(pDialog);
124 
125 	setFieldList();
126 }
127 
setFieldList()128 void AP_Dialog_MailMerge::setFieldList()
129 {
130 	// subclasses must override this
131 	UT_ASSERT_NOT_REACHED();
132 }
133 
addClicked()134 void AP_Dialog_MailMerge::addClicked()
135 {
136 	XAP_Frame * pFrame = m_pFrame;
137 	UT_return_if_fail (pFrame);
138 
139 	FV_View * pView = static_cast<FV_View*>(pFrame->getCurrentView());
140 	UT_return_if_fail (pView);
141 
142 	const gchar * pParam = getMergeField().utf8_str();
143 
144 	if(pParam && *pParam) {
145 	  const gchar * pAttr[3];
146 	  const gchar param_name[] = "param";
147 	  pAttr[0] = static_cast<const gchar *>(&param_name[0]);
148 	  pAttr[1] = pParam;
149 	  pAttr[2] = 0;
150 
151 	  pView->cmdInsertField("mail_merge",static_cast<const gchar **>(&pAttr[0]));
152 	}
153 }
154