void FileUtils::fileCounter(const QString& filePath, int* pCounter) { QFileInfo curFile(filePath); if(!curFile.exists()) return; if (curFile.isDir()) { QString dir = curFile.filePath(); QDir curDir(dir); int childCount = curDir.entryInfoList().count(); QFileInfoList newFileList = curDir.entryInfoList(); if(childCount > 2) { for(int i = 0;i< childCount; i++) { if(newFileList.at(i).fileName() == "." || newFileList.at(i).fileName() == "..") continue; fileCounter(newFileList.at(i).filePath(), pCounter); } } } else { if(pCounter) *pCounter += 1; } }
NS_IMETHODIMP sbLocalDatabaseLibraryLoader::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *aData) { nsresult rv; if (strcmp(aTopic, NS_FINAL_UI_STARTUP_CATEGORY) == 0) { if (m_DetectedCorruptLibrary) { /* check to see if the prefs file is writable, to recover from people * running the app with sudo :( */ nsCOMPtr<nsIProperties> dirService = do_GetService("@mozilla.org/file/directory_service;1", &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIFile> prefFile; rv = dirService->Get("PrefF", NS_GET_IID(nsIFile), getter_AddRefs(prefFile)); NS_ENSURE_SUCCESS(rv, rv); PRBool prefExists; /* if the pref file is missing we go through the normal first-run process * and therefore don't need to care about this */ PRBool prefWritable = PR_TRUE; rv = prefFile->Exists(&prefExists); if (NS_SUCCEEDED(rv) && prefExists) { rv = prefFile->IsWritable(&prefWritable); NS_ENSURE_SUCCESS(rv, rv); } if (prefWritable) { rv = PromptToDeleteLibraries(); NS_ENSURE_SUCCESS(rv, rv); } else { // database file is not writable, just bail rv = PromptInaccessibleLibraries(); NS_ENSURE_SUCCESS(rv, rv); } } } else if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) { // By now, databases should all be closed so it is safe to delete // the db directory. if (m_DeleteLibrariesAtShutdown) { // get profile directory nsCOMPtr<nsIProperties> directoryService( do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv)); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIFile> siteDBDir; rv = directoryService->Get("ProfD", NS_GET_IID(nsIFile), getter_AddRefs(siteDBDir)); NS_ENSURE_SUCCESS(rv, rv); // append the database dir name siteDBDir->Append(NS_LITERAL_STRING("db")); // Delete all the databases but the metrics DB. (which hopefully // is not corrupt) nsCOMPtr<nsISimpleEnumerator> dirEnumerator; rv = siteDBDir->GetDirectoryEntries(getter_AddRefs(dirEnumerator)); NS_ENSURE_SUCCESS(rv, rv); nsString metricsdb = NS_LITERAL_STRING("metrics.db"); PRBool hasMore; dirEnumerator->HasMoreElements(&hasMore); while (hasMore && NS_SUCCEEDED(rv)) { nsCOMPtr<nsISupports> aSupport; rv = dirEnumerator->GetNext(getter_AddRefs(aSupport)); if (NS_FAILED(rv)) break; nsCOMPtr<nsIFile> curFile(do_QueryInterface(aSupport, &rv)); if (NS_FAILED(rv)) break; nsString leafName; rv = curFile->GetLeafName(leafName); if (NS_FAILED(rv)) break; if (leafName.Compare(metricsdb) != 0) { rv = curFile->Remove(false); NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Unable to delete file"); } dirEnumerator->HasMoreElements(&hasMore); } // We want to prompt the user to rescan on restart. nsCAutoString scancompleteBranch("songbird.firstrun.scancomplete"); sbLocalDatabaseLibraryLoader::RemovePrefBranch(scancompleteBranch); // And delete all the library prefs, so they get recreated on // startup. (It would be nice to not need to do this, so that // users could delete their db directory by hand and restart, but // bad things happen due to prefs and it's not worth putting time // into.) nsCAutoString prefBranch(PREFBRANCH_LOADER); sbLocalDatabaseLibraryLoader::RemovePrefBranch(prefBranch); } } return NS_OK; }
void ParseThread::ProcessParseAndStore(ParseRequest* req) { wxString dbfile = req->getDbfile(); // convert the file to tags double maxVal = (double)req->_workspaceFiles.size(); if ( maxVal == 0.0 ) { return; } // we report every 10% double reportingPoint = maxVal / 100.0; reportingPoint = ceil( reportingPoint ); if(reportingPoint == 0.0) { reportingPoint = 1.0; } ITagsStoragePtr db(new TagsStorageSQLite()); db->OpenDatabase( dbfile ); // We commit every 10 files db->Begin(); int precent (0); int lastPercentageReported(0); PPTable::Instance()->Clear(); for (size_t i=0; i<maxVal; i++) { // give a shutdown request a chance if( TestDestroy() ) { // Do an ordered shutdown: // rollback any transaction // and close the database db->Rollback(); return; } wxFileName curFile(wxString(req->_workspaceFiles.at(i).c_str(), wxConvUTF8)); // Skip binary files if(TagsManagerST::Get()->IsBinaryFile(curFile.GetFullPath())) { DEBUG_MESSAGE( wxString::Format(wxT("Skipping binary file %s"), curFile.GetFullPath().c_str()) ); continue; } // Send notification to the main window with our progress report precent = (int)((i / maxVal) * 100); if( req->_evtHandler && lastPercentageReported != precent) { lastPercentageReported = precent; wxCommandEvent retaggingProgressEvent(wxEVT_PARSE_THREAD_RETAGGING_PROGRESS); retaggingProgressEvent.SetInt( (int)precent ); req->_evtHandler->AddPendingEvent(retaggingProgressEvent); } else if(lastPercentageReported != precent) { wxPrintf(wxT("parsing: %%%d completed\n"), precent); } TagTreePtr tree = TagsManagerST::Get()->ParseSourceFile(curFile); PPScan( curFile.GetFullPath(), false ); db->Store(tree, wxFileName(), false); if(db->InsertFileEntry(curFile.GetFullPath(), (int)time(NULL)) == TagExist) { db->UpdateFileEntry(curFile.GetFullPath(), (int)time(NULL)); } if ( i % 50 == 0 ) { // Commit what we got so far db->Commit(); // Start a new transaction db->Begin(); } } // Process the macros PPTable::Instance()->Squeeze(); const std::map<wxString, PPToken>& table = PPTable::Instance()->GetTable(); // Store the macros db->StoreMacros( table ); // Commit whats left db->Commit(); // Clear the results PPTable::Instance()->Clear(); /// Send notification to the main window with our progress report if( req->_evtHandler ) { wxCommandEvent retaggingCompletedEvent(wxEVT_PARSE_THREAD_RETAGGING_COMPLETED); std::vector<std::string> *arrFiles = new std::vector<std::string>; *arrFiles = req->_workspaceFiles; retaggingCompletedEvent.SetClientData( arrFiles ); req->_evtHandler->AddPendingEvent(retaggingCompletedEvent); } }
bool FileTemplater::create() { // output files and output file iterator std::vector<std::ofstream*> outputFiles; for (std::list<std::string>::const_iterator it=m_dynamicFiles.begin(); it!=m_dynamicFiles.end(); ++it) { std::stringstream ss; ss << m_outputDirectory << *it << "." << m_fileExtension; std::ofstream* curFile = new std::ofstream(ss.str().c_str(), std::ofstream::out|std::ofstream::binary); if (!curFile->is_open()) { delete curFile; std::cerr << "Could not open '" << ss.str() << "'" << std::endl; return false; } else if (m_verbose) { std::cout << "Opened file: '" << ss.str() << "'" << std::endl; } outputFiles.push_back(curFile); } // step through 'file order' for (std::list<std::string>::const_iterator it=m_fileOrder.begin(); it!=m_fileOrder.end(); ++it) { if ((*it).empty()) { std::cout << "Ignoring empty file entry" << std::endl; } if ((*it).at(0)=='*') { // dynamic page, build file suffix from iterator std::string fileSuffix = (*it).substr(1); // get 2 iterators, 1 for input & 1 for output std::list<std::string>::const_iterator iIt = m_dynamicFiles.begin(); std::vector<std::ofstream*>::iterator oIt = outputFiles.begin(); for ( ; iIt!=m_dynamicFiles.end(); ++iIt, ++oIt) { std::stringstream ss; ss << m_inputDirectory << *iIt << fileSuffix; std::ifstream curFile(ss.str().c_str(), std::ifstream::in|std::ifstream::binary); if (curFile.is_open()) { if (m_verbose) { std::cout << "Opened dynamic file '" << ss.str() << "'" << std::endl; } appendSingleFile(*oIt, curFile); } else if (m_strict) { std::cerr << "Error, mode is set to strict and input file '" << ss.str() << "' does not exist" << std::endl; it = m_fileOrder.end(); break; } else if (m_verbose) { std::cout << "Skipping '" << ss.str() << "' as it does not exist" << std::endl; } } } else { std::stringstream ss; ss << m_inputDirectory << *it; std::ifstream curFile(ss.str().c_str(), std::ifstream::in|std::ifstream::binary); if (curFile.is_open()) { if (m_verbose) { std::cout << "Opened file '" << ss.str() << "'" << std::endl; } appendToAllFiles(outputFiles, curFile); { } } else { std::cerr << "Could not open required file '" << ss.str() << "'" << std::endl; break; } } } // copy additional files boost::filesystem::path sink(m_outputDirectory); for (std::list<std::string>::const_iterator it=m_additionalFiles.begin(); it!=m_additionalFiles.end(); ++it) { // build up full input file name std::stringstream ss; ss << m_inputDirectory << *it; // check if the file/directory exists boost::filesystem::path source(ss.str()); if (boost::filesystem::exists(source)) { if (boost::filesystem::is_directory(source)) { copyDirectory(source, sink / *it); } else { copyFile(source, sink / source.filename()); } } else { std::cout << "Additional file '" << ss.str() << "' does not exist. Skipping." << std::endl; } } if (m_verbose) { std::cout << "Closing files" << std::endl; } // finally, free data for (std::vector<std::ofstream*>::iterator it=outputFiles.begin(); it!=outputFiles.end(); ++it) { delete *it; } return true; }
QCursor createCursor(QString path) { if (path.isEmpty()) return QCursor(); // read file headers QFile curFile(path); curFile.open(QIODevice::ReadOnly); QDataStream curStream(&curFile); curStream.setByteOrder(QDataStream::LittleEndian); struct { quint16 zero; quint16 type; quint16 icons; } header2; curStream >> header2.zero >> header2.type >> header2.icons; struct { quint8 width; quint8 height; quint8 ncolours; quint8 zero; quint16 xhot; quint16 yhot; quint32 bytes; quint32 dibOffset; } directory2; curStream >> directory2.width >> directory2.height >> directory2.ncolours >> directory2.zero >> directory2.xhot >> directory2.yhot >> directory2.bytes >> directory2.dibOffset; curFile.seek(directory2.dibOffset); // prepare a .bmp for delegating decoding to qt struct { unsigned char magic[2]; quint32 size; quint32 zero; quint32 rdataOffset; } bmpHeader; int bmpHeaderSize = (2+4+4+4); struct { quint32 hdrSize; quint32 width; quint32 height; quint16 planes; quint16 bpp; quint32 compression; quint32 dataSize; quint32 unused1; quint32 unused2; quint32 unused3; quint32 unused4; } dibHeader; int dibHeaderSize = (4+4+4+2+2+4+4+4+4+4+4); bmpHeader.magic[0] = 'B'; bmpHeader.magic[1] = 'M'; bmpHeader.zero = 0; bmpHeader.size = bmpHeaderSize + directory2.bytes; bmpHeader.rdataOffset = bmpHeaderSize + dibHeaderSize + directory2.ncolours * 4; curStream >> dibHeader.hdrSize >> dibHeader.width >> dibHeader.height >> dibHeader.planes >> dibHeader.bpp >> dibHeader.compression >> dibHeader.dataSize >> dibHeader.unused1 >> dibHeader.unused2 >> dibHeader.unused3 >> dibHeader.unused4; dibHeader.height >>= 1; // the bmp bytes are in 'bmpData' QByteArray bmpData; QDataStream bmpStream(&bmpData, QIODevice::WriteOnly); bmpStream.setByteOrder(QDataStream::LittleEndian); bmpStream.writeRawData((char*) bmpHeader.magic, 2); bmpStream << bmpHeader.size << bmpHeader.zero << bmpHeader.rdataOffset; bmpStream << dibHeader.hdrSize << dibHeader.width << dibHeader.height << dibHeader.planes << dibHeader.bpp << dibHeader.compression << dibHeader.dataSize << dibHeader.unused1 << dibHeader.unused2 << dibHeader.unused3 << dibHeader.unused4; bmpData.append(curFile.read(directory2.bytes - dibHeaderSize)); // decode the image into 'pix' int width = directory2.width; int height = directory2.height; QImage image; image.loadFromData(bmpData); //qDebug() << image.rect() << path; QPixmap pix = QPixmap::fromImage(image); // now we need the mask (transparency) QByteArray maskData = bmpData.right((width * height) / 8); QImage maskImage = QBitmap::fromData(QSize(width, height), (const uchar*) maskData.constData(), QImage::Format_Mono).toImage().mirrored(false, true); maskImage.invertPixels(); pix.setMask(QBitmap::fromImage(maskImage)); return QCursor(pix, directory2.xhot, directory2.yhot); }