Can dynamically set show/hide checkboxes in GitStatusListCtrl
[TortoiseGit.git] / src / TortoiseMerge / WorkingFile.cpp
blobdbabc50e47aff7262843be83798e7b7567fe67e3
1 // TortoiseGitMerge - a Diff/Patch program
3 // Copyright (C) 2006-2007, 2011-2012 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "stdafx.h"
20 #include "Workingfile.h"
21 #include "AppUtils.h"
22 #include "PathUtils.h"
23 #include "resource.h"
24 #include "SmartHandle.h"
26 CWorkingFile::CWorkingFile(void)
28 ClearStoredAttributes();
31 CWorkingFile::~CWorkingFile(void)
35 void CWorkingFile::SetFileName(const CString& newFilename)
37 m_sFilename = newFilename;
38 m_sFilename.Replace('/', '\\');
39 m_sDescriptiveName.Empty();
40 ClearStoredAttributes();
43 void CWorkingFile::SetDescriptiveName(const CString& newDescName)
45 m_sDescriptiveName = newDescName;
48 CString CWorkingFile::GetDescriptiveName()
50 if (m_sDescriptiveName.IsEmpty())
52 CString sDescriptiveName = CPathUtils::GetFileNameFromPath(m_sFilename);
53 WCHAR pathbuf[MAX_PATH] = {0};
54 PathCompactPathEx(pathbuf, sDescriptiveName, 50, 0);
55 sDescriptiveName = pathbuf;
56 return sDescriptiveName;
58 return m_sDescriptiveName;
61 // Make an empty file with this name
62 void CWorkingFile::CreateEmptyFile()
64 CAutoFile hFile = CreateFile(m_sFilename, GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
67 // Move the details of the specified file to the current one, and then mark the specified file
68 // as out of use
69 void CWorkingFile::TransferDetailsFrom(CWorkingFile& rightHandFile)
71 // We don't do this to files which are already in use
72 ASSERT(!InUse());
74 m_sFilename = rightHandFile.m_sFilename;
75 m_sDescriptiveName = rightHandFile.m_sDescriptiveName;
76 rightHandFile.SetOutOfUse();
77 m_attribs = rightHandFile.m_attribs;
80 CString CWorkingFile::GetWindowName() const
82 CString sErrMsg = _T("");
83 // TortoiseMerge allows non-existing files to be used in a merge
84 // Inform the user (in a non-intrusive way) if a file is absent
85 if (! this->Exists())
87 sErrMsg = CString(MAKEINTRESOURCE(IDS_NOTFOUNDVIEWTITLEINDICATOR));
90 if(m_sDescriptiveName.IsEmpty())
92 // We don't have a proper name - use the filename part of the path
93 // return the filename part of the path.
94 return CPathUtils::GetFileNameFromPath(m_sFilename) + _T(" ") + sErrMsg;
96 else if (sErrMsg.IsEmpty())
98 return m_sDescriptiveName;
100 return m_sDescriptiveName + _T(" ") + sErrMsg;
103 bool CWorkingFile::Exists() const
105 return (!!PathFileExists(m_sFilename));
108 void CWorkingFile::SetOutOfUse()
110 m_sFilename.Empty();
111 m_sDescriptiveName.Empty();
112 ClearStoredAttributes();
116 bool CWorkingFile::HasSourceFileChanged() const
118 if (!InUse())
120 return false;
122 WIN32_FILE_ATTRIBUTE_DATA attribs = {0};
123 if (PathFileExists(m_sFilename))
125 if (GetFileAttributesEx(m_sFilename, GetFileExInfoStandard, &attribs))
127 if ( (m_attribs.nFileSizeHigh != attribs.nFileSizeHigh) ||
128 (m_attribs.nFileSizeLow != attribs.nFileSizeLow) )
129 return true;
130 return ( (CompareFileTime(&m_attribs.ftCreationTime, &attribs.ftCreationTime)!=0) ||
131 (CompareFileTime(&m_attribs.ftLastWriteTime, &attribs.ftLastWriteTime)!=0) );
135 return false;
138 void CWorkingFile::StoreFileAttributes()
140 ClearStoredAttributes();
142 WIN32_FILE_ATTRIBUTE_DATA attribs = {0};
143 if (GetFileAttributesEx(m_sFilename, GetFileExInfoStandard, &attribs))
145 m_attribs = attribs;
149 void CWorkingFile::ClearStoredAttributes()
151 static const WIN32_FILE_ATTRIBUTE_DATA attribsEmpty = {0};
152 m_attribs = attribsEmpty;