// -*- C++ -*- /* This file is part of * ====================================================== * * LyX, The Document Processor * * Copyright (C) 1995 Matthias Ettrich * Copyright (C) 1995-1998 The LyX Team. *
*======================================================*/
#if HAVE_DIRENT_H # include <dirent.h> # define NAMLEN(dirent) strlen((dirent)->d_name) #else # define dirent direct # define NAMLEN(dirent) (dirent)->d_namlen # if HAVE_SYS_NDIR_H # include <sys/ndir.h> # endif # if HAVE_SYS_DIR_H # include <sys/dir.h> # endif # if HAVE_NDIR_H # include <ndir.h> # endif #endif
#if TIME_WITH_SYS_TIME # include <sys/time.h> # include <time.h> #else # if HAVE_SYS_TIME_H # include <sys/time.h> # else # include <time.h> # endif #endif
// global instance (user cache root)
UserCache lyxUserCache = UserCache(LString(),0,0);
// Add: creates a new user entry
UserCache *UserCache::Add(uid_t ID)
{
LString pszNewName; struct passwd *pEntry;
// gets user name if ((pEntry = getpwuid(ID)))
pszNewName = pEntry->pw_name; else {
pszNewName = LString() + int(ID); // We don't have int cast to LString
}
// adds new node returnnew UserCache(pszNewName, ID, pRoot);
}
// stores data
this->pszName = pszName;
this->ID = ID;
}
UserCache::~UserCache()
{ if (pNext) delete pNext;
}
// Find: seeks user name from user ID
LString UserCache::Find(uid_t ID)
{ if ((!pszName.empty()) && (this->ID == ID)) return pszName; if (pNext) return pNext->Find(ID);
return pRoot->Add(ID)->pszName;
}
// *** Group cache class implementation
// global instance (group cache root)
GroupCache lyxGroupCache = GroupCache(LString(),0,0);
// Add: creates a new group entry
GroupCache *GroupCache::Add(gid_t ID)
{
LString pszNewName; struct group *pEntry;
// gets user name if ((pEntry = getgrgid(ID))) pszNewName = pEntry->gr_name; else {
pszNewName = LString() + int(ID); // We don't have int cast to LString
}
// adds new node returnnew GroupCache(pszNewName, ID, pRoot);
}
// stores data
this->pszName = pszName;
this->ID = ID;
}
GroupCache::~GroupCache()
{ if (pNext) delete pNext;
}
// Find: seeks group name from group ID
LString GroupCache::Find(gid_t ID)
{ if ((!pszName.empty()) && (this->ID == ID)) return pszName; if (pNext) return pNext->Find(ID);
// Reread: updates dialog list to match class directory void LyXFileDlg::Reread()
{ int i;
DIR *pDirectory; struct dirent *pDirEntry;
LString szFile; char szBuffer[256], szMode[15], szTime[40];
FileInfo fileInfo;
// Splits complete directory name into directories and compute depth
iDepth = 0;
LString line, Temp;
szFile = pszDirectory; if (szFile != "/") {
szFile.split(Temp, '/');
} while (!szFile.empty() || !Temp.empty()) {
LString dline = "@b"+line + Temp + '/';
fl_add_browser_line(pFileDlgForm->List, dline.c_str());
szFile.split(Temp, '/');
line += ' ';
iDepth++;
}
// Allocate names array
iNumNames = 0;
rewinddir(pDirectory); while ((readdir(pDirectory))) ++iNumNames;
pCurrentNames = new LyXDirEntry[iNumNames];
// Parses all entries of the given subdirectory
iNumNames = 0;
time_t curTime = time(NULL);
rewinddir(pDirectory); while ((pDirEntry = readdir(pDirectory))) {
bool isLink = false, isDir = false;
// If the pattern doesn't start with a dot, skip hidden files if (!pszMask.empty() && pszMask[0] != '.' &&
pDirEntry->d_name[0] == '.') continue;
if (curTime > fileInfo.getModificationTime() + SIX_MONTH_SEC
|| curTime < fileInfo.getModificationTime()
+ ONE_HOUR_SEC) { // The file is fairly old or in the future. POSIX says // the cutoff is 6 months old. Allow a 1 hour slop // factor for what is considered "the future", to // allow for NFS server/client clock disagreement. // Show the year instead of the time of day.
strcpy(szTime+10, szTime+19);
szTime[15] = 0;
} else
szTime[16] = 0;
// filters files according to pattern and type if (fileInfo.isRegular()
|| fileInfo.isChar()
|| fileInfo.isBlock()
|| fileInfo.isFifo()) { if (!fname.regexMatch(pszMask)) continue;
} elseif (!(isDir = fileInfo.isDir())) continue;
pCurrentNames[iNumNames].pszLsEntry = szBuffer;
// creates used name
LString temp = fname; if (isDir) temp += '/';
pCurrentNames[iNumNames].pszName = temp;
// creates displayed name
temp = pDirEntry->d_name; if (isLink)
temp += '@'; else
temp += fileInfo.typeIndicator();
// Add them to directory box for (i = 0; i < iNumNames; ++i) {
LString temp = line + pCurrentNames[i].pszDisplayed;
fl_add_browser_line(pFileDlgForm->List, temp.c_str());
}
fl_set_browser_topline(pFileDlgForm->List,iDepth);
fl_show_object(pFileDlgForm->List);
iLastSel = -1;
}
// Creates form if necessary. if (!pFileDlgForm) {
pFileDlgForm = create_form_FileDlg(); // Set callbacks. This means that we don't need a patch file
fl_set_object_callback(pFileDlgForm->DirBox,
LyXFileDlg::FileDlgCB,0);
fl_set_object_callback(pFileDlgForm->PatBox,
LyXFileDlg::FileDlgCB,1);
fl_set_object_callback(pFileDlgForm->List,
LyXFileDlg::FileDlgCB,2);
fl_set_object_callback(pFileDlgForm->Filename,
LyXFileDlg::FileDlgCB,3);
fl_set_object_callback(pFileDlgForm->Rescan,
LyXFileDlg::FileDlgCB,10);
fl_set_object_callback(pFileDlgForm->Home,
LyXFileDlg::FileDlgCB,11);
fl_set_object_callback(pFileDlgForm->User1,
LyXFileDlg::FileDlgCB,12);
fl_set_object_callback(pFileDlgForm->User2,
LyXFileDlg::FileDlgCB,13);
// Make sure pressing the close box doesn't crash LyX. (RvdK)
fl_set_form_atclose(pFileDlgForm->FileDlg, CancelCB, NULL); // Register doubleclick callback
fl_set_browser_dblclick_callback(pFileDlgForm->List,
DoubleClickCB,0);
}
fl_hide_object(pFileDlgForm->User1);
fl_hide_object(pFileDlgForm->User2);
}
// XForms objects callback (static) void LyXFileDlg::FileDlgCB(FL_OBJECT *, long lArgument)
{ if (!pCurrentDlg) return;
switch (lArgument) {
case 0: // get directory
pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
pCurrentDlg->Reread(); break;
case 1: // get mask
pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
pCurrentDlg->Reread(); break;
case 2: // list
pCurrentDlg->HandleListHit(); break;
case 10: // rescan
pCurrentDlg->SetDirectory(fl_get_input(pFileDlgForm->DirBox));
pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
pCurrentDlg->Reread(); break;
case 11: // home
pCurrentDlg->SetDirectory(getEnvPath("HOME"));
pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
pCurrentDlg->Reread(); break;
case 12: // user button 1 if (!pCurrentDlg->pszUserPath1.empty()) {
pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath1);
pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
pCurrentDlg->Reread();
} break;
case 13: // user button 2 if (!pCurrentDlg->pszUserPath2.empty()) {
pCurrentDlg->SetDirectory(pCurrentDlg->pszUserPath2);
pCurrentDlg->SetMask(fl_get_input(pFileDlgForm->PatBox));
pCurrentDlg->Reread();
} break;
}
}
// Handle callback from list void LyXFileDlg::HandleListHit()
{ // set info line int iSelect = fl_get_browser(pFileDlgForm->List); if (iSelect > iDepth) {
SetInfoLine(pCurrentNames[iSelect - iDepth - 1].pszLsEntry);
} else {
SetInfoLine(LString());
}
}
// Callback for double click in list void LyXFileDlg::DoubleClickCB(FL_OBJECT *, long)
{ if (pCurrentDlg->HandleDoubleClick()) // Simulate click on OK button
pCurrentDlg->Force(false);
}
// Handle double click from list bool LyXFileDlg::HandleDoubleClick()
{ bool isDir;
LString pszTemp; int iSelect;
// set info line
isDir = true;
iSelect = fl_get_browser(pFileDlgForm->List); if (iSelect > iDepth) {
pszTemp = pCurrentNames[iSelect - iDepth - 1].pszName;
SetInfoLine(pCurrentNames[iSelect - iDepth - 1].pszLsEntry); if (!pszTemp.suffixIs('/')) {
isDir = false;
fl_set_input(pFileDlgForm->Filename, pszTemp.c_str());
}
} elseif (iSelect !=0) {
SetInfoLine(LString());
} else returntrue;
// executes action if (isDir) {
int i;
LString Temp;
// builds new directory name if (iSelect > iDepth) { // Directory deeper down // First, get directory with trailing /
Temp = fl_get_input(pFileDlgForm->DirBox); if (!Temp.suffixIs('/'))
Temp += '/';
Temp += pszTemp;
} else { // Directory higher up
Temp.clean(); for (i = 0; i < iSelect; ++i) {
LString piece = fl_get_browser_line(pFileDlgForm->List, i+1); // The '+2' is here to count the '@b' (JMarc)
Temp += piece.substring(i+2, piece.length()-1);
}
}
// assigns it
SetDirectory(Temp);
Reread(); returnfalse;
} returntrue;
}
// Handle OK button call bool LyXFileDlg::HandleOK()
{
LString pszTemp;
// mask was changed
pszTemp = fl_get_input(pFileDlgForm->PatBox); if (pszTemp!=pszMask) {
SetMask(pszTemp);
Reread(); returnfalse;
}
// directory was changed
pszTemp = fl_get_input(pFileDlgForm->DirBox); if (pszTemp!=pszDirectory) {
SetDirectory(pszTemp);
Reread(); returnfalse;
}
// Handle return from list int select = fl_get_browser(pFileDlgForm->List); if (select > iDepth) {
LString temp = pCurrentNames[select - iDepth - 1].pszName; if (!temp.suffixIs('/')) { // If user didn't type anything, use browser
LString name = fl_get_input(pFileDlgForm->Filename); if (name.empty()) {
fl_set_input(pFileDlgForm->Filename, temp.c_str());
} returntrue;
}
}
// Emulate a doubleclick return HandleDoubleClick();
}
// Handle Cancel CB from WM close int LyXFileDlg::CancelCB(FL_FORM *, void *)
{ // Simulate a click on the cancel button
pCurrentDlg->Force(true); return FL_IGNORE;
}
// Simulates a click on OK/Cancel void LyXFileDlg::Force(bool cancel)
{ if (cancel) {
force_cancel = true;
fl_set_button(pFileDlgForm->Cancel, 1);
} else {
force_ok = true;
fl_set_button(pFileDlgForm->Ready, 1);
} // Start timer to break fl_do_forms loop soon
fl_set_timer(pFileDlgForm->timer, 0.1);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.