00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "precomp.h"
00028 #include "CCPrintData.h"
00029
00030 #include "CCPrintRegistry.h"
00031
00032 #include <time.h>
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00070 #define JOBDATA_TIME_PREFIX _T("CCJobTime")
00072 #define JOBDATA_TIME_KEY JOBDATA_TIME_PREFIX _T("%d")
00074 #define JOBDATA_FILE_KEY _T("CCJobFile%d")
00075
00076
00078 #define DATAFILE_SECTION_MAIN _T("Job")
00080 #define DATAFILE_SECTION_MAIN_WRITE _T("[Job]\r\n")
00082 #define DATAFILE_SECTION_PAGE _T("Page %d")
00084 #define DATAFILE_SECTION_PAGE_WRITE _T("[Page %d]\r\n")
00085
00086
00088 #define DATAFILE_PAGE_COUNT _T("PageCount")
00090 #define DATAFILE_PAGE_COUNT_WRITE _T("PageCount=%d\r\n")
00092 #define DATAFILE_PAGE_TEST _T("TestPage")
00094 #define DATAFILE_PAGE_TEST_WRITE _T("TestPage=%d\r\n")
00095
00096
00098 #define DATAFILE_LINK_COUNT _T("LinkCount")
00100 #define DATAFILE_LINK_COUNT_WRITE _T("LinkCount=%d\r\n")
00101
00102
00104 #define DATAFILE_LINK_URL _T("URL%d")
00106 #define DATAFILE_LINK_TITLE _T("Title%d")
00107
00108
00110 #define DATAFILE_LINK_PAGE _T("Page%d")
00112 #define DATAFILE_LINK_PAGE_WRITE _T("Page%d=%d\r\n")
00114 #define DATAFILE_LINK_OFFSET_X _T("OffsetX%d")
00116 #define DATAFILE_LINK_OFFSET_X_WRITE _T("OffsetX%d=%d\r\n")
00118 #define DATAFILE_LINK_OFFSET_Y _T("OffsetY%d")
00120 #define DATAFILE_LINK_OFFSET_Y_WRITE _T("OffsetY%d=%d\r\n")
00121
00122
00124 #define DATAFILE_LINK_TEXT _T("Text%d")
00126 #define DATAFILE_LINK_REPEAT _T("Repeat%d")
00128 #define DATAFILE_LINK_REPEAT_WRITE _T("Repeat%d=%d\r\n")
00129
00130
00132 #define DATAFILE_LINK_LOC_LEFT _T("Left%d")
00134 #define DATAFILE_LINK_LOC_LEFT_WRITE _T("Left%d=%d\r\n")
00136 #define DATAFILE_LINK_LOC_RIGHT _T("Right%d")
00138 #define DATAFILE_LINK_LOC_RIGHT_WRITE _T("Right%d=%d\r\n")
00140 #define DATAFILE_LINK_LOC_TOP _T("Top%d")
00142 #define DATAFILE_LINK_LOC_TOP_WRITE _T("Top%d=%d\r\n")
00144 #define DATAFILE_LINK_LOC_BOTTOM _T("Bottom%d")
00146 #define DATAFILE_LINK_LOC_BOTTOM_WRITE _T("Bottom%d=%d\r\n")
00147
00148
00150 #define DATAFILE_PAGE_WIDTH _T("Width")
00152 #define DATAFILE_PAGE_WIDTH_WRITE _T("Width=%d\r\n")
00154 #define DATAFILE_PAGE_HEIGHT _T("Height")
00156 #define DATAFILE_PAGE_HEIGHT_WRITE _T("Height=%d\r\n")
00157
00161 void CCPrintData::LinkData::CleanText()
00162 {
00163 std::tstring::size_type pos;
00164 while ((pos = sText.find_first_of(_T("\r\n"))) != std::tstring::npos)
00165 sText.erase(pos, 1);
00166 }
00167
00173 bool CCPrintData::LinkData::FromFile(const TCHARSTR2STR& data, int nNum)
00174 {
00175
00176 TCHAR cName[32];
00177 TCHARSTR2STR::const_iterator iKey;
00178 sText = _T("");
00179 sURL = _T("");
00180 sTitle = _T("");
00181 nPage = 0;
00182
00183
00184 _stprintf_s(cName, _S(cName), DATAFILE_LINK_PAGE, nNum);
00185 if ((iKey = data.find(cName)) != data.end())
00186 {
00187
00188 nPage = _ttoi((*iKey).second.c_str());
00189 if (nPage < 1)
00190 return false;
00191 _stprintf_s(cName, _S(cName), DATAFILE_LINK_OFFSET_X, nNum);
00192 if ((iKey = data.find(cName)) != data.end())
00193 ptOffset.x = _ttoi((*iKey).second.c_str());
00194 else
00195 ptOffset.x = 0;
00196 _stprintf_s(cName, _S(cName), DATAFILE_LINK_OFFSET_Y, nNum);
00197 if ((iKey = data.find(cName)) != data.end())
00198 ptOffset.y = _ttoi((*iKey).second.c_str());
00199 else
00200 ptOffset.y = 0;
00201 }
00202 else
00203 {
00204
00205 _stprintf_s(cName, _S(cName), DATAFILE_LINK_URL, nNum);
00206 if ((iKey = data.find(cName)) == data.end())
00207 return false;
00208
00209 sURL = (*iKey).second;
00210 }
00211
00212
00213 _stprintf_s(cName, _S(cName), DATAFILE_LINK_TITLE, nNum);
00214 if ((iKey = data.find(cName)) != data.end())
00215 sTitle = (*iKey).second;
00216
00217 _stprintf_s(cName, _S(cName), DATAFILE_LINK_TEXT, nNum);
00218 if ((iKey = data.find(cName)) != data.end())
00219 {
00220
00221 sText = (*iKey).second;
00222 _stprintf_s(cName, _S(cName), DATAFILE_LINK_REPEAT, nNum);
00223 if ((iKey = data.find(cName)) != data.end())
00224 {
00225 int nTemp = _ttoi((*iKey).second.c_str());
00226 if (nTemp > 0)
00227 nRepeat = nTemp;
00228 }
00229 }
00230 else
00231 {
00232
00233 _stprintf_s(cName, _S(cName), DATAFILE_LINK_LOC_LEFT, nNum);
00234 if ((iKey = data.find(cName)) == data.end())
00235 return false;
00236 rectLocation.left = _ttoi((*iKey).second.c_str());
00237 _stprintf_s(cName, _S(cName), DATAFILE_LINK_LOC_RIGHT, nNum);
00238 if ((iKey = data.find(cName)) == data.end())
00239 return false;
00240 rectLocation.right = _ttoi((*iKey).second.c_str());
00241 _stprintf_s(cName, _S(cName), DATAFILE_LINK_LOC_TOP, nNum);
00242 if ((iKey = data.find(cName)) == data.end())
00243 return false;
00244 rectLocation.top = _ttoi((*iKey).second.c_str());
00245 _stprintf_s(cName, _S(cName), DATAFILE_LINK_LOC_BOTTOM, nNum);
00246 if ((iKey = data.find(cName)) == data.end())
00247 return false;
00248 rectLocation.bottom = _ttoi((*iKey).second.c_str());
00249 }
00250 return true;
00251 }
00252
00258 bool CCPrintData::LinkData::ToFile(std::tstring& sData, int nNum) const
00259 {
00260 TCHAR cName[64];
00261
00262
00263 if (IsInner())
00264 {
00265
00266 _stprintf_s(cName, _S(cName), DATAFILE_LINK_PAGE_WRITE, nNum, nPage);
00267 sData += cName;
00268 if (ptOffset.x != 0)
00269 {
00270 _stprintf_s(cName, _S(cName), DATAFILE_LINK_OFFSET_X_WRITE, nNum, ptOffset.x);
00271 sData += cName;
00272 }
00273 if (ptOffset.y != 0)
00274 {
00275 _stprintf_s(cName, _S(cName), DATAFILE_LINK_OFFSET_Y_WRITE, nNum, ptOffset.y);
00276 sData += cName;
00277 }
00278 }
00279 else
00280 {
00281
00282 _stprintf_s(cName, _S(cName), DATAFILE_LINK_URL, nNum);
00283 sData += cName;
00284 sData += _T("=") + sURL + _T("\n\r");
00285 }
00286
00287 if (!sTitle.empty())
00288 {
00289
00290 _stprintf_s(cName, _S(cName), DATAFILE_LINK_TITLE, nNum);
00291 sData += cName;
00292 sData += _T("=") + sTitle + _T("\n\r");
00293 }
00294
00295
00296 if (IsLocation())
00297 {
00298
00299 _stprintf_s(cName, _S(cName), DATAFILE_LINK_LOC_LEFT_WRITE, nNum, rectLocation.left);
00300 sData += cName;
00301 _stprintf_s(cName, _S(cName), DATAFILE_LINK_LOC_RIGHT_WRITE, nNum, rectLocation.right);
00302 sData += cName;
00303 _stprintf_s(cName, _S(cName), DATAFILE_LINK_LOC_TOP_WRITE, nNum, rectLocation.top);
00304 sData += cName;
00305 _stprintf_s(cName, _S(cName), DATAFILE_LINK_LOC_BOTTOM_WRITE, nNum, rectLocation.bottom);
00306 sData += cName;
00307 }
00308 else
00309 {
00310
00311 _stprintf_s(cName, _S(cName), DATAFILE_LINK_TEXT, nNum);
00312 sData += cName;
00313 sData += _T("=") + sText + _T("\n\r");
00314 if (nRepeat > 1)
00315 {
00316
00317 _stprintf_s(cName, _S(cName), DATAFILE_LINK_REPEAT_WRITE, nNum, nRepeat);
00318 sData += cName;
00319 }
00320 }
00321 return true;
00322 }
00323
00324
00325
00331 bool CCPrintData::PageData::FromFile(class FileINI& file, int nPage)
00332 {
00333
00334 TCHAR cName[32];
00335 TCHARSTR2STR data;
00336 TCHARSTR2STR::const_iterator iKey;
00337
00338
00339 Clear();
00340
00341
00342 _stprintf_s(cName, _S(cName), DATAFILE_SECTION_PAGE, nPage);
00343 if (!file.GetKeys(cName, data) || ((iKey = data.find(DATAFILE_LINK_COUNT)) == data.end()))
00344
00345 return true;
00346
00347
00348 int nLinkCount = _ttoi((*iKey).second.c_str());
00349 if (nLinkCount < 1)
00350
00351 return false;
00352
00353
00354 for (int iLink = 1; iLink <= nLinkCount; iLink++)
00355 {
00356 LinkData link;
00357 if (link.FromFile(data, iLink))
00358 push_back(link);
00359 }
00360
00361
00362 if ((iKey = data.find(DATAFILE_PAGE_WIDTH)) != data.end())
00363 szPage.cx = _ttoi((*iKey).second.c_str());
00364 if ((iKey = data.find(DATAFILE_PAGE_HEIGHT)) != data.end())
00365 szPage.cy = _ttoi((*iKey).second.c_str());
00366
00367 return true;
00368 }
00369
00375 bool CCPrintData::PageData::ToFile(std::tstring& sData, int nPage) const
00376 {
00377
00378 if (empty())
00379 return true;
00380
00381
00382 TCHAR cName[64];
00383 _stprintf_s(cName, _S(cName), DATAFILE_SECTION_PAGE_WRITE, nPage);
00384 sData += cName;
00385
00386 _stprintf_s(cName, _S(cName), DATAFILE_LINK_COUNT_WRITE, size());
00387 sData += cName;
00388
00389
00390 int iLink = 1;
00391 for (const_iterator i = begin(); i != end(); i++, iLink++)
00392 if (!(*i).ToFile(sData, iLink))
00393 return false;
00394
00395 if (szPage.cx != 0)
00396 {
00397
00398 _stprintf_s(cName, _S(cName), DATAFILE_PAGE_WIDTH_WRITE, szPage.cx);
00399 sData += cName;
00400 }
00401 if (szPage.cy != 0)
00402 {
00403
00404 _stprintf_s(cName, _S(cName), DATAFILE_PAGE_HEIGHT_WRITE, szPage.cy);
00405 sData += cName;
00406 }
00407
00408 return true;
00409 }
00410
00411
00416 bool CCPrintData::UpdateProcessData(HANDLE hPrinter)
00417 {
00418
00419 TCHAR cKeyFile[64];
00420 DWORD dwProcessID = GetCurrentProcessId();
00421
00422
00423 _stprintf_s(cKeyFile, _S(cKeyFile), JOBDATA_FILE_KEY, dwProcessID);
00424 std::tstring sFilename = CCPrintRegistry::GetRegistryString(hPrinter, cKeyFile, _T(""));
00425 if (sFilename.empty())
00426
00427 return false;
00428
00429
00430 return WriteToFile(sFilename.c_str());
00431 }
00432
00437 bool CCPrintData::ReloadProcessData(HANDLE hPrinter)
00438 {
00439
00440 TCHAR cKeyFile[64];
00441 DWORD dwProcessID = GetCurrentProcessId();
00442
00443
00444 _stprintf_s(cKeyFile, _S(cKeyFile), JOBDATA_FILE_KEY, dwProcessID);
00445 std::tstring sFilename = CCPrintRegistry::GetRegistryString(hPrinter, cKeyFile, _T(""));
00446 if (sFilename.empty())
00447
00448 return false;
00449
00450
00451 return ReadFromFile(sFilename.c_str());
00452 }
00453
00458 bool CCPrintData::LoadProcessData(HANDLE hPrinter)
00459 {
00460
00461 TCHAR cKeyTime[64], cKeyFile[64];
00462 DWORD dwProcessID = GetCurrentProcessId(), dwTimeKey;
00463 time_t tNow, tKey;
00464
00465
00466 CleanThis();
00467 CleanOldData(hPrinter);
00468
00469
00470 _stprintf_s(cKeyTime, _S(cKeyTime), JOBDATA_TIME_KEY, dwProcessID);
00471 if ((dwTimeKey = CCPrintRegistry::GetRegistryDWORD(hPrinter, cKeyTime, 0)) == 0)
00472
00473 return false;
00474
00475
00476 tKey = (time_t)dwTimeKey;
00477 tNow = time(NULL);
00478
00479
00480 bool bRet = false;
00481 if (difftime(tNow, tKey) < 300)
00482 {
00483
00484 _stprintf_s(cKeyFile, _S(cKeyFile), JOBDATA_FILE_KEY, dwProcessID);
00485 std::tstring sFilename = CCPrintRegistry::GetRegistryString(hPrinter, cKeyFile, _T(""));
00486 if (!sFilename.empty())
00487
00488 bRet = ReadFromFile(sFilename.c_str());
00489 }
00490
00491 return bRet;
00492 }
00493
00498 bool CCPrintData::ReadFromFile(LPCTSTR lpFilename)
00499 {
00500
00501 FileINI file;
00502 if (!file.LoadINIFile(lpFilename))
00503 return false;
00504
00505 TCHARSTR2STR data;
00506 TCHARSTR2STR::const_iterator iKey;
00507
00508
00509 file.GetKeys(DATAFILE_SECTION_MAIN, data);
00510 if ((iKey = data.find(DATAFILE_PAGE_TEST)) != data.end())
00511
00512 m_bTestPage = _ttoi((*iKey).second.c_str()) != 0;
00513
00514
00515 iKey = data.find(DATAFILE_PAGE_COUNT);
00516 if (iKey != data.end())
00517 {
00518 int nCount = _ttoi((*iKey).second.c_str());
00519 if (nCount > 0)
00520 {
00521
00522 m_pages.resize(nCount);
00523 for (int nPage = 1; nPage <= nCount; nPage++)
00524 m_pages[nPage-1].FromFile(file, nPage);
00525 }
00526 }
00527
00528 return true;
00529 }
00530
00534 void CCPrintData::CleanOldData(HANDLE hPrinter)
00535 {
00536
00537 STRLIST lValues;
00538 if (!CCPrintRegistry::EnumRegistryValues(hPrinter, lValues, JOBDATA_TIME_PREFIX))
00539
00540 return;
00541
00542 DWORD dwTimeKey, dwProcessID;
00543 time_t tNow, tKey;
00544
00545
00546 for (STRLIST::const_iterator i = lValues.begin(); i != lValues.end(); i++)
00547 {
00548
00549 if ((dwTimeKey = CCPrintRegistry::GetRegistryDWORD(hPrinter, (*i).c_str(), 0)) == 0)
00550
00551 continue;
00552
00553 tKey = (time_t)dwTimeKey;
00554 tNow = time(NULL);
00555
00556
00557 if (difftime(tNow, tKey) > 300)
00558 {
00559
00560 dwProcessID = _ttoi((*i).c_str() + _tcslen(JOBDATA_TIME_PREFIX));
00561 if (dwProcessID == 0)
00562 continue;
00563
00564 CleanData(hPrinter, dwProcessID);
00565 }
00566 }
00567 }
00568
00573 void CCPrintData::CleanData(HANDLE hPrinter, DWORD dwProcessID)
00574 {
00575 TCHAR cKey[64];
00576
00577 _stprintf_s(cKey, _S(cKey), JOBDATA_FILE_KEY, dwProcessID);
00578 std::tstring sFilename = CCPrintRegistry::GetRegistryString(hPrinter, cKey, _T(""));
00579
00580 if (!sFilename.empty())
00581 _tunlink(sFilename.c_str());
00582
00583 CCPrintRegistry::EraseRegistryValue(hPrinter, cKey);
00584
00585 _stprintf_s(cKey, _S(cKey), JOBDATA_TIME_KEY, dwProcessID);
00586 CCPrintRegistry::EraseRegistryValue(hPrinter, cKey);
00587 }
00588
00592 void CCPrintData::CleanSaved(HANDLE hPrinter)
00593 {
00594
00595 CleanData(hPrinter, GetCurrentProcessId());
00596 }
00597
00602 bool CCPrintData::SaveProcessData(HANDLE hPrinter)
00603 {
00604
00605 DWORD dwProcessID = GetCurrentProcessId();
00606
00607
00608 CleanData(hPrinter, dwProcessID);
00609
00610
00611
00612 TCHAR cFilename[MAX_PATH + 1], cPath[MAX_PATH + 1], cFileKey[64], cTimeKey[64];
00613 if (GetTempPath(MAX_PATH, cPath) == 0)
00614 return false;
00615 if (GetTempFileName(cPath, _T("CCPDF"), 0, cFilename) == 0)
00616 return false;
00617
00618
00619 if (!WriteToFile(cFilename))
00620 {
00621
00622 _tunlink(cFilename);
00623 return false;
00624 }
00625
00626
00627 _stprintf_s(cTimeKey, _S(cTimeKey), JOBDATA_TIME_KEY, dwProcessID);
00628 if (!CCPrintRegistry::SetRegistryDWORD(hPrinter, cTimeKey, (DWORD) time(NULL)))
00629 {
00630 _tunlink(cFilename);
00631 return false;
00632 }
00633
00634 _stprintf_s(cFileKey, _S(cFileKey), JOBDATA_FILE_KEY, dwProcessID);
00635 if (!CCPrintRegistry::SetRegistryString(hPrinter, cFileKey, cFilename))
00636 {
00637 CCPrintRegistry::EraseRegistryValue(hPrinter, cTimeKey);
00638 _tunlink(cFilename);
00639 return false;
00640 }
00641 return true;
00642 }
00643
00648 bool CCPrintData::WriteToFile(LPCTSTR lpFilename)
00649 {
00650
00651 std::tstring sData;
00652 TCHAR cName[64];
00653
00654
00655 sData += DATAFILE_SECTION_MAIN_WRITE;
00656
00657
00658 _stprintf_s(cName, _S(cName), DATAFILE_PAGE_COUNT_WRITE, m_pages.size());
00659 sData += cName;
00660 if (m_bTestPage)
00661 {
00662
00663 _stprintf_s(cName, _S(cName), DATAFILE_PAGE_TEST_WRITE, 1);
00664 sData += cName;
00665 }
00666
00667
00668 for (unsigned int iPage = 0; iPage < m_pages.size(); iPage++)
00669 if (!m_pages[iPage].ToFile(sData, iPage + 1))
00670 return false;
00671
00672
00673 FILE* pFile;
00674 if (NULL != _tfopen_s(&pFile, lpFilename, _T("wt")))
00675 return false;
00676 #ifdef _UNICODE
00677
00678 unsigned char cUni[2];
00679 cUni[0] = 0xFF;
00680 cUni[1] = 0xFE;
00681 if (fwrite(cUni, sizeof(unsigned char), 2, pFile) != 2)
00682 {
00683 fclose(pFile);
00684 return false;
00685 }
00686 #endif
00687
00688 if (fwrite(sData.c_str(), sizeof(TCHAR), sData.size(), pFile) != sData.size())
00689 {
00690 fclose(pFile);
00691 return false;
00692 }
00693
00694
00695 fclose(pFile);
00696 return true;
00697 }
00698
00702 void CCPrintData::EnsurePage(int nPage)
00703 {
00704 while ((int)m_pages.size() < nPage)
00705 m_pages.push_back(PageData());
00706 }
00707
00714 void CCPrintData::AddLink(const std::tstring& sURL, const std::tstring& sText, int nPage, int nRepeat )
00715 {
00716
00717 EnsurePage(nPage);
00718
00719 m_pages[nPage - 1].push_back(LinkData(sURL, sText, nRepeat));
00720 }
00721
00728 void CCPrintData::AddLink(const std::tstring& sURL, const RECTL& rect, int nPage, LPCTSTR lpTitle )
00729 {
00730
00731 EnsurePage(nPage);
00732
00733 m_pages[nPage - 1].push_back(LinkData(sURL, rect, lpTitle));
00734 }
00735
00744 void CCPrintData::AddLink(const RECTL& rect, int nPage, int nDestPage, long lX, long lY, LPCTSTR lpTitle )
00745 {
00746
00747 EnsurePage(nPage);
00748
00749 m_pages[nPage - 1].push_back(LinkData(rect, nDestPage, lX, lY, lpTitle));
00750 }
00751
00752 #ifdef _DEBUG
00753
00756 void CCPrintData::Dump()
00757 {
00758 std::tstring sData;
00759 TCHAR cName[64];
00760 sData += DATAFILE_SECTION_MAIN_WRITE;
00761 _stprintf_s(cName, _S(cName), DATAFILE_PAGE_COUNT_WRITE, m_pages.size());
00762 sData += cName;
00763 if (m_bTestPage)
00764 {
00765 _stprintf_s(cName, _S(cName), DATAFILE_PAGE_TEST_WRITE, 1);
00766 sData += cName;
00767 }
00768 for (unsigned int iPage = 0; iPage < m_pages.size(); iPage++)
00769 if (!m_pages[iPage].ToFile(sData, iPage + 1))
00770 return;
00771
00772 ::OutputDebugString(sData.c_str());
00773 }
00774 #endif