Subject: Patch: Bug 1550
From: Andrew Dunbar (hippietrail@yahoo.com)
Date: Wed Jun 20 2001 - 01:15:54 CDT
Here is a workaround for 1550. I can't call this an actual fix
since we really need to rethink the relationships between filetypes,
importers, exporters, file extensions, and the SaveAs dialog.
But now we'll be doing the write thing in 99.99% of cases. I think
will also fix the bug about auto-saved backups not being loadable.
Andrew Dunbar.
-- http://linguaphile.sourceforge.net
Index: src/af/xap/xp/xad_Document.h =================================================================== RCS file: /cvsroot/abi/src/af/xap/xp/xad_Document.h,v retrieving revision 1.19 diff -u -r1.19 xad_Document.h --- src/af/xap/xp/xad_Document.h 2001/06/12 20:09:42 1.19 +++ src/af/xap/xp/xad_Document.h 2001/06/20 04:50:58 @@ -37,7 +37,7 @@ void unref(void); const char * getFilename(void) const; - virtual UT_uint32 getLastType() = 0; + virtual UT_uint32 getLastSavedAsType() = 0; // TODO - this should be returning IEFileType, // but that's AP stuff, so it's not here Index: src/af/xap/xp/xap_Frame.cpp =================================================================== RCS file: /cvsroot/abi/src/af/xap/xp/xap_Frame.cpp,v retrieving revision 1.69 diff -u -r1.69 xap_Frame.cpp --- src/af/xap/xp/xap_Frame.cpp 2001/06/13 01:23:36 1.69 +++ src/af/xap/xp/xap_Frame.cpp 2001/06/20 04:51:04 @@ -731,7 +731,7 @@ UT_DEBUGMSG(("Filename [%s]\n", oldName.c_str())); backupName = oldName + ext; - UT_Error error = m_pDoc->saveAs(backupName.c_str(), m_pDoc->getLastType(), false); + UT_Error error = m_pDoc->saveAs(backupName.c_str(), m_pDoc->getLastSavedAsType(), false); UT_DEBUGMSG(("File %s saved.\n", backupName.c_str())); m_bBackupRunning = false; Index: src/text/ptbl/xp/pd_Document.cpp =================================================================== RCS file: /cvsroot/abi/src/text/ptbl/xp/pd_Document.cpp,v retrieving revision 1.119 diff -u -r1.119 pd_Document.cpp --- src/text/ptbl/xp/pd_Document.cpp 2001/06/18 12:58:37 1.119 +++ src/text/ptbl/xp/pd_Document.cpp 2001/06/20 04:51:15 @@ -51,8 +51,8 @@ #include "fl_AutoNum.h" #include "xap_App.h" #include "ut_units.h" +#include "ut_string_class.h" - ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// @@ -62,9 +62,6 @@ void* pToken; }; -// quick hack, not really dirty though :) -#define IEFT_AbiWord_1 IE_Exp::fileTypeForSuffix (".abw") - ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// @@ -77,7 +74,8 @@ m_ballowListUpdates(false), m_pPieceTable(0), m_hashDataItems(11), - m_lastSavedAsType(IEFT_AbiWord_1), + m_lastOpenedType(IE_Imp::fileTypeForSuffix(".abw")), + m_lastSavedAsType(IE_Exp::fileTypeForSuffix(".abw")), m_bPieceTableChanging(false), m_bDoingPaste(false), m_bAllowInsertPointChange(true) @@ -170,13 +168,15 @@ IE_Imp * pie = NULL; UT_Error errorCode; - errorCode = IE_Imp::constructImporter(this, szFilename, (IEFileType) ieft, &pie, &m_lastSavedAsType); + errorCode = IE_Imp::constructImporter(this, szFilename, (IEFileType) ieft, &pie, &m_lastOpenedType); if (errorCode) { UT_DEBUGMSG(("PD_Document::readFromFile -- could not construct importer\n")); return errorCode; } + _syncFileTypes(false); + m_pPieceTable->setPieceTableState(PTS_Loading); errorCode = pie->importFile(szFilename); delete pie; @@ -242,6 +242,8 @@ return UT_SAVE_EXPORTERROR; } + _syncFileTypes(true); + errorCode = pie->writeFile(szFilename); delete pie; @@ -292,6 +294,8 @@ return UT_SAVE_EXPORTERROR; } + _syncFileTypes(true); + errorCode = pie->writeFile(m_szFilename); delete pie; @@ -1310,6 +1314,48 @@ } } +/*! + Synchronize the last opened/last saves filetypes. + \param bBool bOpenedFromSaved True to set opened from saved, otherwise the reverse + + There are actually two filetypes - one for importers and one for + exporters. This function tries to synchronize the one to the other. +*/ +bool PD_Document::_syncFileTypes(bool bOpenedFromSaved) +{ + const char *szSuffixes; + + if (bOpenedFromSaved) + szSuffixes = IE_Exp::suffixesForFileType(m_lastSavedAsType); + else + szSuffixes = IE_Imp::suffixesForFileType(m_lastOpenedType); + + // Pull first suffix from the file dialog pattern string + UT_String suffix; + for (const char *p = szSuffixes; *p && *p != ';'; ++p) + if (*p != '*') + suffix += *p; + + IEFileType ieft; + if (bOpenedFromSaved) + { + ieft = IE_Imp::fileTypeForSuffix(suffix.c_str()); + m_lastOpenedType = ieft; + } + else + { + ieft = IE_Exp::fileTypeForSuffix(suffix.c_str()); + m_lastSavedAsType = ieft; + } + + if (ieft == IEFT_Unknown || ieft == IEFT_Bogus) + { + UT_ASSERT(UT_SHOULD_NOT_HAPPEN); + return false; + } + + return true; +} /////////////////////////////////////////////////////////////////// // Styles represent named collections of formatting properties. Index: src/text/ptbl/xp/pd_Document.h =================================================================== RCS file: /cvsroot/abi/src/text/ptbl/xp/pd_Document.h,v retrieving revision 1.88 diff -u -r1.88 pd_Document.h --- src/text/ptbl/xp/pd_Document.h 2001/06/12 20:10:01 1.88 +++ src/text/ptbl/xp/pd_Document.h 2001/06/20 04:51:18 @@ -188,7 +188,7 @@ void clearIfAtFmtMark(PT_DocPosition dpos); const char * getFileName() { return m_szFilename; } - UT_uint32 getLastType() { return m_lastSavedAsType; } + UT_uint32 getLastSavedAsType() { return m_lastSavedAsType; } XAP_App * getApp() { return m_pApp; } bool updateFields(void); bool getField(PL_StruxDocHandle sdh, @@ -240,6 +240,7 @@ void _setClean(void); void _destroyDataItemData(void); + bool _syncFileTypes(bool bOpenedFromSaved); bool m_ballowListUpdates; pt_PieceTable * m_pPieceTable; UT_Vector m_vecListeners; @@ -247,6 +248,7 @@ UT_StringPtrMap m_hashDataItems; + IEFileType m_lastOpenedType; IEFileType m_lastSavedAsType; XAP_App * m_pApp; bool m_bPieceTableChanging; Index: src/wp/ap/xp/ap_Convert.cpp =================================================================== RCS file: /cvsroot/abi/src/wp/ap/xp/ap_Convert.cpp,v retrieving revision 1.2 diff -u -r1.2 ap_Convert.cpp --- src/wp/ap/xp/ap_Convert.cpp 2001/03/25 07:46:36 1.2 +++ src/wp/ap/xp/ap_Convert.cpp 2001/06/20 04:51:23 @@ -22,6 +22,7 @@ #include "ap_Convert.h" #include "ie_exp.h" +#include "ie_imp.h" #include "ut_types.h" class XAP_App; @@ -128,7 +129,7 @@ strncat(ext, szTargetSuffix, 255); strncat(sourceExt, szSourceSuffix, 255); ieft = IE_Exp::fileTypeForSuffix(ext); - sourceIeft = IE_Exp::fileTypeForSuffix(sourceExt); + sourceIeft = IE_Imp::fileTypeForSuffix(sourceExt); strncpy(file, szFilename, 255); tmp = strrchr(file, '.'); Index: src/wp/ap/xp/ap_EditMethods.cpp =================================================================== RCS file: /cvsroot/abi/src/wp/ap/xp/ap_EditMethods.cpp,v retrieving revision 1.350 diff -u -r1.350 ap_EditMethods.cpp --- src/wp/ap/xp/ap_EditMethods.cpp 2001/06/20 04:35:03 1.350 +++ src/wp/ap/xp/ap_EditMethods.cpp 2001/06/20 04:51:42 @@ -1202,8 +1202,10 @@ if (*ieft != IEFT_Bogus) dflFileType = *ieft; - else + else if (bSaveAs) dflFileType = IE_Exp::fileTypeForSuffix (".abw"); + else + dflFileType = IE_Imp::fileTypeForSuffix (".abw"); pDialog->setDefaultFileType(dflFileType); Index: src/wp/impexp/xp/ie_exp.cpp =================================================================== RCS file: /cvsroot/abi/src/wp/impexp/xp/ie_exp.cpp,v retrieving revision 1.50 diff -u -r1.50 ie_exp.cpp --- src/wp/impexp/xp/ie_exp.cpp 2001/05/25 18:02:35 1.50 +++ src/wp/impexp/xp/ie_exp.cpp 2001/06/20 04:51:47 @@ -279,6 +279,15 @@ /*****************************************************************/ /*****************************************************************/ +/*! + Find the filetype for the given suffix. + \param szSuffix File suffix + + Returns IEFT_AbiWord_1 if no exporter knows this suffix. + Note that more than one exporter may support a suffix. + We return the first one we find. + This function should closely resemble IE_Exp::fileTypeForSuffix() +*/ IEFileType IE_Exp::fileTypeForSuffix(const char * szSuffix) { if (!szSuffix) @@ -291,13 +300,13 @@ for (UT_uint32 k=0; k < nrElements; k++) { - IE_ExpSniffer * s = (IE_ExpSniffer*) m_sniffers.getNthItem(k); + IE_ExpSniffer * s = static_cast<IE_ExpSniffer*>(m_sniffers.getNthItem(k)); if (s->recognizeSuffix(szSuffix)) { for (UT_uint32 a = 0; a < nrElements; a++) { - if (s->supportsFileType((IEFileType) a+1)) - return (IEFileType) a+1; + if (s->supportsFileType(static_cast<IEFileType>(a+1))) + return static_cast<IEFileType>(a+1); } UT_ASSERT(UT_SHOULD_NOT_HAPPEN); @@ -314,24 +323,68 @@ } +/*! + Find the suffixes for the given filetype. + \param szSuffix File suffix + + Returns 0 if no exporter knows this filetype. + This function should closely resemble IE_Exp::suffixesForFileType() +*/ +const char * IE_Exp::suffixesForFileType(IEFileType ieft) +{ + const char * szSuffixes = 0; + + // we have to construct the loop this way because a + // given filter could support more than one file type, + // so we must query a suffix match for all file types + UT_uint32 nrElements = getExporterCount(); + + for (UT_uint32 k=0; k < nrElements; k++) + { + IE_ExpSniffer * s = static_cast<IE_ExpSniffer*>(m_sniffers.getNthItem(k)); + if (s->supportsFileType(ieft)) + { + const char *szDummy; + IEFileType ieftDummy; + if (s->getDlgLabels(&szDummy,&szSuffixes,&ieftDummy)) + return szSuffixes; + else + UT_ASSERT(UT_SHOULD_NOT_HAPPEN); + } + } + + // The passed in filetype is invalid. + return 0; +} + +/*! + Construct an exporter of the right type. + \param pDocument Document + \param szFilename Name of file - optional + \param ieft Desired filetype - pass IEFT_Unknown for best guess + \param ppie Pointer to return importer in + \param pieft Pointer to fill in actual filetype + + Caller is responsible for deleting the exporter object + when finished with it. + This function should closely match IE_Imp::contructImporter() +*/ UT_Error IE_Exp::constructExporter(PD_Document * pDocument, const char * szFilename, IEFileType ieft, IE_Exp ** ppie, IEFileType * pieft) { - // construct the right type of exporter. - // caller is responsible for deleing the exporter object - // when finished with it. + bool bUseGuesswork = (ieft != IEFT_Unknown); UT_ASSERT(pDocument); - UT_ASSERT(szFilename && *szFilename); + UT_ASSERT(ieft != IEFT_Unknown || (szFilename && *szFilename)); UT_ASSERT(ppie); // no filter will support IEFT_Unknown, so we detect from the // suffix of the filename, the real exporter to use and assign // that back to ieft. - if (ieft == IEFT_Unknown) + if (ieft == IEFT_Unknown && szFilename && *szFilename) { ieft = IE_Exp::fileTypeForSuffix(UT_pathSuffix(szFilename)); } Index: src/wp/impexp/xp/ie_exp.h =================================================================== RCS file: /cvsroot/abi/src/wp/impexp/xp/ie_exp.h,v retrieving revision 1.20 diff -u -r1.20 ie_exp.h --- src/wp/impexp/xp/ie_exp.h 2001/06/08 16:07:10 1.20 +++ src/wp/impexp/xp/ie_exp.h 2001/06/20 04:51:48 @@ -77,6 +77,7 @@ // with it. static IEFileType fileTypeForSuffix(const char * szSuffix); + static const char * suffixesForFileType(IEFileType ieft); static UT_Error constructExporter(PD_Document * pDocument, const char * szFilename, Index: src/wp/impexp/xp/ie_imp.cpp =================================================================== RCS file: /cvsroot/abi/src/wp/impexp/xp/ie_imp.cpp,v retrieving revision 1.46 diff -u -r1.46 ie_imp.cpp --- src/wp/impexp/xp/ie_imp.cpp 2001/06/18 15:11:47 1.46 +++ src/wp/impexp/xp/ie_imp.cpp 2001/06/20 04:51:54 @@ -127,6 +127,15 @@ } +/*! + Find the filetype for the given suffix. + \param szSuffix File suffix + + Returns IEFT_Unknown if no importer knows this suffix. + Note that more than one importer may support a suffix. + We return the first one we find. + This function should closely resemble IE_Exp::fileTypeForSuffix() +*/ IEFileType IE_Imp::fileTypeForSuffix(const char * szSuffix) { if (!szSuffix) @@ -139,13 +148,13 @@ for (UT_uint32 k=0; k < nrElements; k++) { - IE_ImpSniffer * s = (IE_ImpSniffer *)m_sniffers.getNthItem (k); + IE_ImpSniffer * s = static_cast<IE_ImpSniffer *>(m_sniffers.getNthItem(k)); if (s->recognizeSuffix(szSuffix)) { for (UT_sint32 a = 0; a < (int) nrElements; a++) { - if (s->supportsFileType((IEFileType) (a+1))) - return (IEFileType) (a+1); + if (s->supportsFileType(static_cast<IEFileType>(a+1))) + return static_cast<IEFileType>(a+1); } UT_ASSERT(UT_SHOULD_NOT_HAPPEN); @@ -161,6 +170,40 @@ } /*! + Find the suffixes for the given filetype. + \param szSuffix File suffix + + Returns 0 if no exporter knows this filetype. + This function should closely resemble IE_Exp::suffixesForFileType() +*/ +const char * IE_Imp::suffixesForFileType(IEFileType ieft) +{ + const char * szSuffixes = 0; + + // we have to construct the loop this way because a + // given filter could support more than one file type, + // so we must query a suffix match for all file types + UT_uint32 nrElements = getImporterCount(); + + for (UT_uint32 k=0; k < nrElements; k++) + { + IE_ImpSniffer * s = static_cast<IE_ImpSniffer*>(m_sniffers.getNthItem(k)); + if (s->supportsFileType(ieft)) + { + const char *szDummy; + IEFileType ieftDummy; + if (s->getDlgLabels(&szDummy,&szSuffixes,&ieftDummy)) + return szSuffixes; + else + UT_ASSERT(UT_SHOULD_NOT_HAPPEN); + } + } + + // The passed in filetype is invalid. + return 0; +} + +/*! Construct an importer of the right type. \param pDocument Document \param szFilename Name of file - optional @@ -170,6 +213,7 @@ Caller is responsible for deleting the importer object when finished with it. + This function should closely match IE_Exp::contructExporter() */ UT_Error IE_Imp::constructImporter(PD_Document * pDocument, const char * szFilename, Index: src/wp/impexp/xp/ie_imp.h =================================================================== RCS file: /cvsroot/abi/src/wp/impexp/xp/ie_imp.h,v retrieving revision 1.21 diff -u -r1.21 ie_imp.h --- src/wp/impexp/xp/ie_imp.h 2001/06/18 15:11:47 1.21 +++ src/wp/impexp/xp/ie_imp.h 2001/06/20 04:51:55 @@ -75,6 +75,7 @@ UT_uint32 iNumbytes); static IEFileType fileTypeForSuffix(const char * szSuffix); + static const char * suffixesForFileType(IEFileType ieft); static UT_Error constructImporter(PD_Document * pDocument, const char * szFilename, Index: src/wp/impexp/xp/ie_imp_Text.cpp =================================================================== RCS file: /cvsroot/abi/src/wp/impexp/xp/ie_imp_Text.cpp,v retrieving revision 1.30 diff -u -r1.30 ie_imp_Text.cpp --- src/wp/impexp/xp/ie_imp_Text.cpp 2001/06/18 15:11:47 1.30 +++ src/wp/impexp/xp/ie_imp_Text.cpp 2001/06/20 04:52:11 @@ -226,24 +226,17 @@ /*****************************************************************/ /*! - Check buffer for identifiable encoded characters - \param szBuf Buffer to check - \param iNumbytes Size of buffer + Check if buffer contains data meant for this importer. + + We don't attmpt to recognize since other filetypes (HTML) can + use the same encodings a text file can. + We also don't want to steal recognition when user wants to use + the Encoded Text importer. */ -bool IE_Imp_Text_Sniffer::recognizeContents(const char * szBuf, - UT_uint32 iNumbytes) +bool IE_Imp_Text_Sniffer::recognizeContents(const char * /* szBuf */, + UT_uint32 /* iNumbytes */) { - // TODO It may or may not be worthwhile trying to recognize CJK encodings. - - bool bSuccess = false; - - bSuccess = _recognizeUTF8(szBuf, iNumbytes); - - if (bSuccess == false) - if (_recognizeUCS2(szBuf, iNumbytes, false) != UE_NotUCS) - bSuccess = true; - - return bSuccess; + return false; } /*! @@ -422,6 +415,17 @@ } /*! + Check if buffer contains data meant for this importer. + + We don't attempt to recognize. User must specifically choose Encoded Text. + */ +bool IE_Imp_EncodedText_Sniffer::recognizeContents(const char * /* szBuf */, + UT_uint32 /* iNumbytes */) +{ + return false; +} + +/*! Check filename extension for filetypes we support \param szSuffix Filename extension */ @@ -469,6 +473,7 @@ return UT_IE_FILENOTFOUND; } + ImportStream *pStream = 0; UT_Error error; // First we try to determine the encoding. @@ -478,7 +483,6 @@ // Call encoding dialog if (!m_bIsEncoded || _doEncodingDialog(m_szEncoding)) { - ImportStream *pStream = 0; X_CleanupIfError(error,_constructStream(pStream,fp)); Inserter ins(m_pDocument); X_CleanupIfError(error,_writeHeader(fp)); @@ -489,6 +493,7 @@ error = UT_ERROR; Cleanup: + delete pStream; fclose(fp); return error; } Index: src/wp/impexp/xp/ie_imp_Text.h =================================================================== RCS file: /cvsroot/abi/src/wp/impexp/xp/ie_imp_Text.h,v retrieving revision 1.17 diff -u -r1.17 ie_imp_Text.h --- src/wp/impexp/xp/ie_imp_Text.h 2001/06/18 15:11:47 1.17 +++ src/wp/impexp/xp/ie_imp_Text.h 2001/06/20 04:52:12 @@ -121,7 +121,7 @@ // The importer/reader for Plain Text Files with selectable encoding. -class IE_Imp_EncodedText_Sniffer : public IE_Imp_Text_Sniffer +class IE_Imp_EncodedText_Sniffer : public IE_ImpSniffer { friend class IE_Imp; friend class IE_Imp_Text; @@ -130,6 +130,8 @@ IE_Imp_EncodedText_Sniffer() {} virtual ~IE_Imp_EncodedText_Sniffer() {} + virtual bool recognizeContents (const char * szBuf, + UT_uint32 iNumbytes); virtual bool recognizeSuffix (const char * szSuffix); virtual bool getDlgLabels (const char ** szDesc, const char ** szSuffixList,
_________________________________________________________ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com
This archive was generated by hypermail 2b25 : Wed Jun 20 2001 - 08:30:03 CDT