| 1 |
/* AbiWord |
| 2 |
* Copyright (C) 2004 Tomas Frydrych <[email protected]> |
| 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 "ut_uuid.h" |
| 21 |
|
| 22 |
#include "xap_Dlg_DocComparison.h" |
| 23 |
#include "xad_Document.h" |
| 24 |
#include "xap_App.h" |
| 25 |
#include "xap_Strings.h" |
| 26 |
|
| 27 |
#include <locale.h> |
| 28 |
|
| 29 |
XAP_Dialog_DocComparison::XAP_Dialog_DocComparison(XAP_DialogFactory * pDlgFactory, XAP_Dialog_Id id): |
| 30 |
XAP_Dialog_NonPersistent(pDlgFactory,id), |
| 31 |
m_pDoc1(NULL), |
| 32 |
m_pDoc2(NULL), |
| 33 |
m_pSS(NULL), |
| 34 |
m_iVersionOfDiff(0), |
| 35 |
m_tTimeOfDiff(0), |
| 36 |
m_iPosOfDiff(0), |
| 37 |
m_iPosOfFmtDiff(0), |
| 38 |
m_bStylesEqual(false) |
| 39 |
{ |
| 40 |
m_pSS = XAP_App::getApp()->getStringSet(); |
| 41 |
} |
| 42 |
|
| 43 |
/*! |
| 44 |
returns true on success |
| 45 |
*/ |
| 46 |
bool XAP_Dialog_DocComparison::calculate(AD_Document * pDoc1, AD_Document * pDoc2) |
| 47 |
{ |
| 48 |
UT_return_val_if_fail(pDoc1 && pDoc2, false); |
| 49 |
m_pDoc1 = pDoc1; |
| 50 |
m_pDoc2 = pDoc2; |
| 51 |
bool bEqual = false; |
| 52 |
|
| 53 |
if(pDoc1->areDocumentsRelated(*pDoc2)) |
| 54 |
{ |
| 55 |
bEqual = pDoc1->areDocumentHistoriesEqual(*pDoc2, m_iVersionOfDiff); |
| 56 |
|
| 57 |
if(!bEqual) |
| 58 |
{ |
| 59 |
const AD_VersionData * v = pDoc1->findHistoryRecord(m_iVersionOfDiff); |
| 60 |
if(v) |
| 61 |
{ |
| 62 |
m_tTimeOfDiff = v->getTime(); |
| 63 |
} |
| 64 |
else |
| 65 |
{ |
| 66 |
UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN); |
| 67 |
m_iVersionOfDiff = 0; |
| 68 |
} |
| 69 |
} |
| 70 |
else |
| 71 |
{ |
| 72 |
m_iVersionOfDiff = 0xffffffff; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if(!bEqual) |
| 77 |
{ |
| 78 |
m_bStylesEqual = pDoc1->areDocumentStylesheetsEqual(*pDoc2); |
| 79 |
|
| 80 |
if(pDoc1->areDocumentContentsEqual(*pDoc2, m_iPosOfDiff)) |
| 81 |
{ |
| 82 |
m_iPosOfDiff = 0xffffffff; |
| 83 |
if(pDoc1->areDocumentFormatsEqual(*pDoc2, m_iPosOfFmtDiff)) |
| 84 |
{ |
| 85 |
m_iPosOfFmtDiff = 0xffffffff; |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
const char * XAP_Dialog_DocComparison::getWindowLabel() const |
| 95 |
{ |
| 96 |
UT_return_val_if_fail(m_pSS, NULL); |
| 97 |
return m_pSS->getValue(XAP_STRING_ID_DLG_DocComparison_WindowLabel); |
| 98 |
} |
| 99 |
|
| 100 |
const char * XAP_Dialog_DocComparison::getFrame1Label() const |
| 101 |
{ |
| 102 |
UT_return_val_if_fail(m_pSS, NULL); |
| 103 |
return m_pSS->getValue(XAP_STRING_ID_DLG_DocComparison_DocsCompared); |
| 104 |
} |
| 105 |
|
| 106 |
const char * XAP_Dialog_DocComparison::getFrame2Label() const |
| 107 |
{ |
| 108 |
UT_return_val_if_fail(m_pSS, NULL); |
| 109 |
return m_pSS->getValue(XAP_STRING_ID_DLG_DocComparison_Results); |
| 110 |
} |
| 111 |
|
| 112 |
static char * s_makePath(const char * pPath) |
| 113 |
{ |
| 114 |
if(!pPath) |
| 115 |
return NULL; |
| 116 |
|
| 117 |
UT_uint32 iPathLen = strlen(pPath); |
| 118 |
UT_String s; |
| 119 |
|
| 120 |
if(iPathLen < 60) |
| 121 |
{ |
| 122 |
UT_String_sprintf(s, "%s", pPath); |
| 123 |
} |
| 124 |
else |
| 125 |
{ |
| 126 |
char * pP1 = g_strdup(pPath); |
| 127 |
pP1[6] = 0; |
| 128 |
const char * pP2 = pPath + iPathLen - 50; |
| 129 |
UT_String_sprintf(s, "%s ... %s", pP1, pP2); |
| 130 |
FREEP(pP1); |
| 131 |
} |
| 132 |
|
| 133 |
return g_strdup(s.c_str()); |
| 134 |
} |
| 135 |
|
| 136 |
char * XAP_Dialog_DocComparison::getPath1() const |
| 137 |
{ |
| 138 |
UT_return_val_if_fail(m_pDoc1, NULL); |
| 139 |
return s_makePath(m_pDoc1->getFilename()); |
| 140 |
} |
| 141 |
|
| 142 |
char * XAP_Dialog_DocComparison::getPath2() const |
| 143 |
{ |
| 144 |
UT_return_val_if_fail(m_pDoc2, NULL); |
| 145 |
return s_makePath(m_pDoc2->getFilename()); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
const char * XAP_Dialog_DocComparison::getResultLabel(UT_uint32 indx) const |
| 150 |
{ |
| 151 |
UT_return_val_if_fail(m_pSS, NULL); |
| 152 |
switch(indx) |
| 153 |
{ |
| 154 |
case 0: return m_pSS->getValue(XAP_STRING_ID_DLG_DocComparison_Relationship); |
| 155 |
case 1: return m_pSS->getValue(XAP_STRING_ID_DLG_DocComparison_Content); |
| 156 |
case 2: return m_pSS->getValue(XAP_STRING_ID_DLG_DocComparison_Fmt); |
| 157 |
case 3: return m_pSS->getValue(XAP_STRING_ID_DLG_DocComparison_Styles); |
| 158 |
default:; |
| 159 |
} |
| 160 |
|
| 161 |
UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN); |
| 162 |
return NULL; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
char * XAP_Dialog_DocComparison::getResultValue(UT_uint32 indx) const |
| 167 |
{ |
| 168 |
UT_return_val_if_fail(m_pSS, NULL); |
| 169 |
|
| 170 |
UT_String S1, S2; |
| 171 |
struct tm * tM; |
| 172 |
char * s; |
| 173 |
|
| 174 |
switch(indx) |
| 175 |
{ |
| 176 |
case 0: // relationship |
| 177 |
if(m_iVersionOfDiff == 0xffffffff) |
| 178 |
{ |
| 179 |
// equal |
| 180 |
return g_strdup(m_pSS->getValue(XAP_STRING_ID_DLG_DocComparison_Identical)); |
| 181 |
} |
| 182 |
else if(m_iVersionOfDiff == 0) |
| 183 |
{ |
| 184 |
// unrelated |
| 185 |
return g_strdup(m_pSS->getValue(XAP_STRING_ID_DLG_DocComparison_Unrelated)); |
| 186 |
} |
| 187 |
else |
| 188 |
{ |
| 189 |
// siblings |
| 190 |
S1 = m_pSS->getValue(XAP_STRING_ID_DLG_DocComparison_Siblings); |
| 191 |
S1 += "; "; |
| 192 |
S1 += m_pSS->getValue(XAP_STRING_ID_DLG_DocComparison_Diverging); |
| 193 |
|
| 194 |
tM = localtime(&m_tTimeOfDiff); |
| 195 |
s = (char*)g_try_malloc(30); |
| 196 |
strftime(s,30,"%c",tM); |
| 197 |
|
| 198 |
UT_String_sprintf(S2, S1.c_str(), m_iVersionOfDiff, s); |
| 199 |
FREEP(s); |
| 200 |
|
| 201 |
return g_strdup(S2.c_str()); |
| 202 |
} |
| 203 |
|
| 204 |
case 1: // content |
| 205 |
if(m_iVersionOfDiff == 0xffffffff) |
| 206 |
{ |
| 207 |
// test skipped |
| 208 |
return g_strdup(m_pSS->getValue(XAP_STRING_ID_DLG_DocComparison_TestSkipped)); |
| 209 |
} |
| 210 |
else if(m_iPosOfDiff == 0xffffffff) |
| 211 |
{ |
| 212 |
// equal |
| 213 |
return g_strdup(m_pSS->getValue(XAP_STRING_ID_DLG_DocComparison_Identical)); |
| 214 |
} |
| 215 |
else |
| 216 |
{ |
| 217 |
const char *s2 = m_pSS->getValue(XAP_STRING_ID_DLG_DocComparison_DivergingPos); |
| 218 |
UT_String_sprintf(S2, s2, m_iPosOfDiff); |
| 219 |
|
| 220 |
return g_strdup(S2.c_str()); |
| 221 |
} |
| 222 |
|
| 223 |
case 2: // fmt |
| 224 |
if(m_iVersionOfDiff == 0xffffffff || m_iPosOfDiff != 0xffffffff) |
| 225 |
{ |
| 226 |
// either the docs are know to be equal, or the |
| 227 |
// content is not known not to be equal |
| 228 |
// test skipped |
| 229 |
return g_strdup(m_pSS->getValue(XAP_STRING_ID_DLG_DocComparison_TestSkipped)); |
| 230 |
} |
| 231 |
else if(m_iPosOfFmtDiff == 0xffffffff) |
| 232 |
{ |
| 233 |
// equal |
| 234 |
return g_strdup(m_pSS->getValue(XAP_STRING_ID_DLG_DocComparison_Identical)); |
| 235 |
} |
| 236 |
else |
| 237 |
{ |
| 238 |
const char * s2 = m_pSS->getValue(XAP_STRING_ID_DLG_DocComparison_DivergingPos); |
| 239 |
UT_String_sprintf(S2, s2, m_iPosOfFmtDiff); |
| 240 |
|
| 241 |
return g_strdup(S2.c_str()); |
| 242 |
} |
| 243 |
|
| 244 |
case 3: // styles |
| 245 |
if(m_iVersionOfDiff == 0xffffffff) |
| 246 |
{ |
| 247 |
// test skipped |
| 248 |
return g_strdup(m_pSS->getValue(XAP_STRING_ID_DLG_DocComparison_TestSkipped)); |
| 249 |
} |
| 250 |
else if(m_bStylesEqual) |
| 251 |
{ |
| 252 |
// equal |
| 253 |
return g_strdup(m_pSS->getValue(XAP_STRING_ID_DLG_DocComparison_Identical)); |
| 254 |
} |
| 255 |
else |
| 256 |
{ |
| 257 |
return g_strdup(m_pSS->getValue(XAP_STRING_ID_DLG_DocComparison_Different)); |
| 258 |
} |
| 259 |
|
| 260 |
default:; |
| 261 |
} |
| 262 |
|
| 263 |
UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN); |
| 264 |
return NULL; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
const char * XAP_Dialog_DocComparison::getButtonLabel() const |
| 269 |
{ |
| 270 |
UT_return_val_if_fail(m_pSS, NULL); |
| 271 |
return m_pSS->getValue(XAP_STRING_ID_DLG_Close); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|