You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
198 lines
6.2 KiB
198 lines
6.2 KiB
/***************************************************************************
|
|
* Copyright (C) 2007 by Andreas Eckstein *
|
|
* andreas.eckstein@gmx.net *
|
|
* *
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
* it under the terms of the GNU General Public License as published by *
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
* (at your option) any later version. *
|
|
* *
|
|
* This program is distributed in the hope that it will be useful, *
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
* GNU General Public License for more details. *
|
|
* *
|
|
* You should have received a copy of the GNU General Public License *
|
|
* along with this program; if not, write to the *
|
|
* Free Software Foundation, Inc., *
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
***************************************************************************/
|
|
#include <qregexp.h>
|
|
#include <kdesktopfile.h>
|
|
|
|
#include "findimpl.h"
|
|
|
|
const TQString FindImpl::history_dir_name=TQString(".find_history.d");
|
|
|
|
void FindImpl::applyFilter(ResultList& list, FindURL& url)
|
|
{
|
|
if(!url.hasFilter())
|
|
return;
|
|
|
|
TQString filter = url.filter().replace('.', "\\.");
|
|
filter = filter.replace('*', "[^\\/]*") + "$";
|
|
TQRegExp re(filter, false);
|
|
ResultListIterator iter = ResultListIterator(list);
|
|
|
|
while(iter.current() != NULL)
|
|
{
|
|
if(re.searchRev((*iter)->path)==-1)
|
|
list.remove(*iter);
|
|
else
|
|
++iter;
|
|
}
|
|
}
|
|
|
|
static void addAtom(TDEIO::UDSEntry &entry, unsigned int ID, long l, const TQString &s = TQString::null)
|
|
{
|
|
TDEIO::UDSAtom atom;
|
|
atom.m_uds = ID;
|
|
atom.m_long = l;
|
|
atom.m_str = s;
|
|
entry.append(atom);
|
|
}
|
|
|
|
FindImpl::FindImpl()
|
|
{
|
|
history_dir.setPath(TQDir::homeDirPath() + "/" + history_dir_name);
|
|
}
|
|
|
|
void FindImpl::appendToHistory(FindURL& furl)
|
|
{
|
|
TQFile _history_file(history_dir.path() + "/" + furl.prettyURL().replace('/', "_"));
|
|
|
|
if(!history_dir.exists())
|
|
if(!TQDir::home().mkdir(history_dir_name))
|
|
return;
|
|
|
|
if(_history_file.open(IO_WriteOnly|IO_Append))
|
|
{
|
|
TQTextStream stream(&_history_file);
|
|
_history_file.close();
|
|
}
|
|
}
|
|
|
|
void FindImpl::parseHistory(TQValueList<TDEIO::UDSEntry> &list)
|
|
{
|
|
history_dir.refresh();
|
|
|
|
if(!history_dir.exists())
|
|
return;
|
|
|
|
if(history_dir.count()==0)
|
|
return;
|
|
|
|
TQFileInfoListIterator it(*history_dir.entryInfoList());
|
|
TQFileInfo *info;
|
|
|
|
while(it.current() != NULL)
|
|
{
|
|
info=*it;
|
|
if(info->isFile())
|
|
{
|
|
FindURL url(info->baseName().replace('_', "/"));
|
|
TDEIO::UDSEntry entry;
|
|
addAtom(entry, TDEIO::UDS_URL, 0, url.prettyURL());
|
|
addAtom(entry, TDEIO::UDS_NAME, 0, url.search());
|
|
addAtom(entry, TDEIO::UDS_FILE_TYPE, S_IFDIR);
|
|
addAtom(entry, TDEIO::UDS_MODIFICATION_TIME, info->lastModified().toTime_t(), TQString::null);
|
|
addAtom(entry, TDEIO::UDS_ACCESS_TIME, info->lastRead().toTime_t(), TQString::null);
|
|
addAtom(entry, TDEIO::UDS_CREATION_TIME, info->created().toTime_t(), TQString::null);
|
|
addAtom(entry, TDEIO::UDS_MIME_TYPE, 0, "inode/directory");
|
|
addAtom(entry, TDEIO::UDS_ICON_NAME, 0, getIcon(url));
|
|
list.append(entry);
|
|
}
|
|
|
|
++it;
|
|
}
|
|
}
|
|
|
|
bool FindImpl::getBasename(TQString& path, TQString& basename)
|
|
{
|
|
int i = path.findRev('/');
|
|
if (i > 0)
|
|
{
|
|
basename = path.mid(i+1);
|
|
}
|
|
else
|
|
{
|
|
basename = path;
|
|
}
|
|
|
|
return basename != TQString::null;
|
|
}
|
|
|
|
static void addSearchEntry(TQValueList<TDEIO::UDSEntry> &list, TQString& path, TQString& label)
|
|
{
|
|
TDEIO::UDSEntry entry;
|
|
addAtom(entry, TDEIO::UDS_URL, 0, path);
|
|
addAtom(entry, TDEIO::UDS_NAME, 0, label);
|
|
|
|
list.append(entry);
|
|
}
|
|
|
|
bool FindImpl::listSearch(FindURL& furl, TQValueList<TDEIO::UDSEntry> &list)
|
|
{
|
|
uint count=0;
|
|
TrackerFindClient tfclient;
|
|
|
|
if(!tfclient.search(furl))
|
|
return false;
|
|
|
|
applyFilter(tfclient.results, furl);
|
|
|
|
ResultListIterator iter = ResultListIterator(tfclient.results);
|
|
|
|
for(; iter.current() != NULL; ++iter)
|
|
{
|
|
TQString path = "file://" + (*iter)->path;
|
|
TQString basename;
|
|
if(getBasename(path, basename))
|
|
addSearchEntry(list, path, basename);
|
|
count++;
|
|
}
|
|
|
|
if(count>0)
|
|
//We have at least one result, so we'll append the URL to the history file
|
|
appendToHistory(furl);
|
|
|
|
return true;
|
|
}
|
|
|
|
bool FindImpl::listSearches(TQValueList<TDEIO::UDSEntry> &list)
|
|
{
|
|
parseHistory(list);
|
|
|
|
return true;
|
|
}
|
|
|
|
void FindImpl::createTopLevelEntry(TDEIO::UDSEntry &entry) const
|
|
{
|
|
entry.clear();
|
|
addAtom(entry, TDEIO::UDS_NAME, 0, ".");
|
|
addAtom(entry, TDEIO::UDS_FILE_TYPE, S_IFDIR);
|
|
addAtom(entry, TDEIO::UDS_ACCESS, 0555);
|
|
addAtom(entry, TDEIO::UDS_MIME_TYPE, 0, "inode/directory");
|
|
addAtom(entry, TDEIO::UDS_ICON_NAME, 0, "find");
|
|
addAtom(entry, TDEIO::UDS_USER, 0, "root");
|
|
addAtom(entry, TDEIO::UDS_GROUP, 0, "root");
|
|
}
|
|
|
|
void FindImpl::createSearchSecondLevelEntry(TDEIO::UDSEntry &entry, TQString searchstring) const
|
|
{
|
|
entry.clear();
|
|
addAtom(entry, TDEIO::UDS_NAME, 0, ".");
|
|
addAtom(entry, TDEIO::UDS_FILE_TYPE, S_IFDIR);
|
|
addAtom(entry, TDEIO::UDS_ACCESS, 0555);
|
|
addAtom(entry, TDEIO::UDS_MIME_TYPE, 0, "inode/directory");
|
|
addAtom(entry, TDEIO::UDS_ICON_NAME, 0, "kfind");
|
|
addAtom(entry, TDEIO::UDS_USER, 0, "root");
|
|
addAtom(entry, TDEIO::UDS_GROUP, 0, "root");
|
|
}
|
|
|
|
const char* FindImpl::getIcon(FindURL &url)
|
|
{
|
|
ServiceType type = TrackerFindClient::getServiceType(url);
|
|
return TrackerFindClient::getServiceIcon(type);
|
|
}
|