예제 #1
0
파일: WndLibrary.cpp 프로젝트: GetEnvy/Envy
BOOL CLibraryWnd::OnCollection(LPCTSTR pszPath)
{
	CAlbumFolder* pFolder = NULL;
	CLibraryFolder* pLibFolder;
	CCollectionFile pCollection;
	CString strMessage;

	if ( ! pCollection.Open( pszPath ) )	// Verify specified collection is valid
	{
		// User clicked an invalid collection
		strMessage.Format( LoadString( IDS_LIBRARY_COLLECTION_INVALID ), pszPath );
		MsgBox( strMessage, MB_ICONEXCLAMATION );
		return FALSE;
	}

	// Create the collection folder (in case it doesn't exist)
	CreateDirectory( Settings.Downloads.CollectionPath );
	// Add the collection folder to the library (in case it isn't there)
	pLibFolder = LibraryFolders.AddFolder( Settings.Downloads.CollectionPath );
	// Force a scan of it (in case watch library folders is disabled)
	pLibFolder = LibraryFolders.GetFolder( Settings.Downloads.CollectionPath );
	if ( pLibFolder != NULL ) pLibFolder->Scan();

	CSingleLock oLock( &Library.m_pSection, TRUE );
	if ( CLibraryFile* pFile = LibraryMaps.LookupFileByPath( pszPath, FALSE, TRUE ) )
	{
		// Collection IS already in the library

		// Re-mount the collection
		LibraryFolders.MountCollection( pFile->m_oSHA1, &pCollection );
		pFolder = LibraryFolders.GetCollection( pFile->m_oSHA1 );
		oLock.Unlock();
	}
	else	// Collection is not already in the main library
	{
		oLock.Unlock();
		// Check the collection folder
		CString strSource( pszPath ), strTarget;

		const int nName = strSource.ReverseFind( L'\\' );
		if ( nName >= 0 )
		{
			strTarget = Settings.Downloads.CollectionPath + strSource.Mid( nName );
			LibraryBuilder.RequestPriority( strTarget );
		}

		oLock.Lock();
		if ( CLibraryFile* pTargetFile = LibraryMaps.LookupFileByPath( strTarget, FALSE, TRUE ) )
		{
			// Collection is already in the collection folder

			// Re-mount the collection
			LibraryFolders.MountCollection( pTargetFile->m_oSHA1, &pCollection );
			pFolder = LibraryFolders.GetCollection( pTargetFile->m_oSHA1 );
			oLock.Unlock();
		}
		else	// Collection is not already in collection folder
		{
			oLock.Unlock();

			if ( ! strTarget.IsEmpty() && CopyFile( strSource, strTarget, TRUE ) )
			{
				// Collection was copied into the collection folder

				// Force a scan of collection folder (in case watch library folders is disabled)
				if ( pLibFolder != NULL ) pLibFolder->Scan();

				strMessage.Format( LoadString( IDS_LIBRARY_COLLECTION_INSTALLED ), (LPCTSTR)pCollection.GetTitle() );
				MsgBox( strMessage, MB_ICONINFORMATION );

				oLock.Lock();
				if ( CLibraryFolder* pCollectionFolder = LibraryFolders.GetFolder(Settings.Downloads.CollectionPath ) )
				{
					pCollectionFolder->Scan();
				}
				if ( CLibraryFile* pTargetFile1 = LibraryMaps.LookupFileByPath( strTarget, FALSE, TRUE ) )
				{
					// Re-mount the collection
					LibraryFolders.MountCollection( pTargetFile1->m_oSHA1, &pCollection );
					pFolder = LibraryFolders.GetCollection( pTargetFile1->m_oSHA1 );
				}
				oLock.Unlock();
			}
			else if ( GetLastError() == ERROR_FILE_EXISTS )
			{
				// File with this name already exists:
				// We cannot copy the collection because it's already there, but it doesn't appear in the library.
				// The greatest probablility is that the file is there, but hasn't been added yet.
				// Best bet is to pretend everything is okay, since the delay it takes the user to respond may fix everything.

				strMessage.Format( LoadString( IDS_LIBRARY_COLLECTION_INSTALLED ), (LPCTSTR)pCollection.GetTitle() );
				MsgBox( strMessage, MB_ICONINFORMATION );

				oLock.Lock();
				if ( CLibraryFile* pTargetFile1 = LibraryMaps.LookupFileByPath( strTarget, FALSE, TRUE ) )
				{
					// Collection was already there: Re-mount the collection
					LibraryFolders.MountCollection( pTargetFile1->m_oSHA1, &pCollection );
					pFolder = LibraryFolders.GetCollection( pTargetFile1->m_oSHA1 );
					oLock.Unlock();
				}
				else	// File of this name exists in the folder, but does not appear in the library.
				{
					// Most likely cause- Corrupt file in collection folder.
					oLock.Unlock();
					strMessage.Format( LoadString( IDS_LIBRARY_COLLECTION_CANT_INSTALL ), (LPCTSTR)pCollection.GetTitle(), (LPCTSTR)Settings.Downloads.CollectionPath );
					MsgBox( strMessage, MB_ICONEXCLAMATION );
				}
			}
			else	// Was not able to copy collection to the collection folder for Unknown reason -Display an error message
			{
				strMessage.Format( LoadString( IDS_LIBRARY_COLLECTION_CANT_INSTALL ), (LPCTSTR)pCollection.GetTitle(), (LPCTSTR)Settings.Downloads.CollectionPath );
				MsgBox( strMessage, MB_ICONEXCLAMATION );
			}
		}
	}

	if ( pFolder )
		Display( pFolder ); 	// Show the collection

	return ( pFolder != NULL );
}