1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Word Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-1999 The LyX Team.
9 * This file is Copyright 1996-1999
12 * ======================================================
16 #pragma implementation
20 #include <sys/types.h>
22 #include "bufferlist.h"
24 #include "minibuffer.h"
25 #include "support/FileInfo.h"
26 #include "support/filetools.h"
27 #include "lyx_gui_misc.h"
28 #include "lastfiles.h"
31 #include "lyxscreen.h"
36 extern BufferView * current_view;
37 extern MiniBuffer * minibuffer;
38 extern void SmallUpdate(signed char);
39 extern void BeforeChange();
40 extern int RunLinuxDoc(int, string const &);
43 // Class BufferStorage
46 void BufferStorage::release(Buffer * buf)
48 for(Container::iterator it = container.begin();
49 it != container.end(); ++it) {
51 Buffer * tmpbuf = (*it);
60 Buffer * BufferStorage::newBuffer(string const & s,
64 Buffer * tmpbuf = new Buffer(s, lyxrc, ronly);
65 tmpbuf->params.useClassDefaults();
66 lyxerr.debug() << "Assigning to buffer "
67 << container.size() + 1 << endl;
68 container.push_back(tmpbuf);
77 BufferList::BufferList()
79 _state = BufferList::OK;
83 bool BufferList::empty()
85 return bstore.empty();
89 extern void MenuWrite(Buffer *);
91 bool BufferList::QwriteAll()
93 bool askMoreConfirmation = false;
95 for(BufferStorage::iterator it = bstore.begin();
96 it != bstore.end(); ++it) {
97 if (!(*it)->isLyxClean()) {
98 switch(AskConfirmation(_("Changes in document:"),
99 MakeDisplayPath((*it)->fileName(),
101 _("Save document?"))) {
106 askMoreConfirmation = true;
107 unsaved += MakeDisplayPath((*it)->fileName(), 50);
115 if (askMoreConfirmation &&
116 lyxrc->exit_confirmation &&
117 !AskQuestion(_("Some documents were not saved:"),
118 unsaved, _("Exit anyway?"))) {
126 // Should probably be moved to somewhere else: BufferView? LyXView?
127 bool BufferList::write(Buffer * buf, bool makeBackup)
129 minibuffer->Set(_("Saving document"),
130 MakeDisplayPath(buf->fileName()), "...");
132 // We don't need autosaves in the immediate future. (Asger)
133 buf->resetAutosaveTimers();
137 string s = buf->fileName() + '~';
138 // Rename is the wrong way of making a backup,
139 // this is the correct way.
140 /* truss cp fil fil2:
141 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
142 stat("LyXVC.lyx", 0xEFFFF688) = 0
143 open("LyXVC.lyx", O_RDONLY) = 3
144 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
145 fstat(4, 0xEFFFF508) = 0
146 fstat(3, 0xEFFFF508) = 0
147 read(3, " # T h i s f i l e w".., 8192) = 5579
148 write(4, " # T h i s f i l e w".., 5579) = 5579
149 read(3, 0xEFFFD4A0, 8192) = 0
152 chmod("LyXVC3.lyx", 0100644) = 0
153 lseek(0, 0, SEEK_CUR) = 46440
157 // Should proabaly have some more error checking here.
158 // Should be cleaned up in 0.13, at least a bit.
159 // Doing it this way, also makes the inodes stay the same.
160 // This is still not a very good solution, in particular we
161 // might loose the owner of the backup.
162 FileInfo finfo(buf->fileName());
164 mode_t fmode = finfo.getMode();
165 struct utimbuf * times = new struct utimbuf;
167 times->actime = finfo.getAccessTime();
168 times->modtime = finfo.getModificationTime();
169 long blksize = finfo.getBlockSize();
170 lyxerr.debug() << "BlockSize: " << blksize << endl;
171 FilePtr fin(buf->fileName(), FilePtr::read);
172 FilePtr fout(s, FilePtr::truncate);
173 if (fin() && fout()) {
174 char * cbuf = new char[blksize+1];
178 c_read = fread(cbuf, 1, blksize, fin);
181 fwrite(cbuf, 1, c_read, fout);
185 chmod(s.c_str(), fmode);
187 if (utime(s.c_str(), times)) {
188 lyxerr << "utime error." << endl;
192 lyxerr << "LyX was not able to make backupcopy. Beware." << endl;
198 if (buf->writeFile(buf->fileName(), false)) {
201 minibuffer->Set(_("Document saved as"),
202 MakeDisplayPath(buf->fileName()));
204 // now delete the autosavefile
205 string a = OnlyPath(buf->fileName());
207 a += OnlyFilename(buf->fileName());
209 FileInfo fileinfo(a);
210 if (fileinfo.exist()) {
211 if (remove(a.c_str()) != 0) {
212 WriteFSAlert(_("Could not delete "
213 "auto-save file!"), a);
217 // Saving failed, so backup is not backup
219 string s = buf->fileName() + '~';
220 rename(s.c_str(), buf->fileName().c_str());
222 minibuffer->Set(_("Save failed!"));
230 void BufferList::closeAll()
232 _state = BufferList::CLOSING;
233 while (!bstore.empty()) {
234 close(bstore.front());
236 _state = BufferList::OK;
240 void BufferList::resize()
242 for(BufferStorage::iterator it = bstore.begin();
243 it != bstore.end(); ++it) {
249 bool BufferList::close(Buffer * buf)
253 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
255 switch(AskConfirmation(_("Changes in document:"),
256 MakeDisplayPath(buf->fileName(), 50),
257 _("Save document?"))){
260 lastfiles->newFile(buf->fileName());
278 void BufferList::makePup(int pup)
279 /* This should be changed to return a char const *const
280 in the same way as for lastfiles.[hC]
284 for(BufferStorage::iterator it = bstore.begin();
285 it != bstore.end(); ++it) {
286 string relbuf = MakeDisplayPath((*it)->fileName(), 30);
287 fl_addtopup(pup, relbuf.c_str());
290 if (ant == 0) fl_addtopup(pup, _("No Documents Open!%t"));
294 Buffer * BufferList::first()
296 if (bstore.empty()) return 0;
297 return bstore.front();
301 Buffer * BufferList::getBuffer(int choice)
303 if (choice >= bstore.size()) return 0;
304 return bstore[choice];
308 void BufferList::updateInset(Inset * inset, bool mark_dirty)
310 for (BufferStorage::iterator it = bstore.begin();
311 it != bstore.end(); ++it) {
313 if ((*it)->getUser() && (*it)->getUser()->text->UpdateInset(inset)) {
319 if ((*it)->text && (*it)->text->UpdateInset(inset)) {
329 int BufferList::unlockInset(UpdatableInset * inset)
331 if (!inset) return 1;
332 for(BufferStorage::iterator it = bstore.begin();
333 it != bstore.end(); ++it) {
334 if ((*it)->the_locking_inset == inset) {
335 (*it)->InsetUnlock();
343 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
345 for(BufferStorage::iterator it = bstore.begin();
346 it != bstore.end(); ++it) {
347 if (!(*it)->isDepClean(mastertmpdir)) {
348 string writefile = mastertmpdir;
350 writefile += ChangeExtension((*it)->fileName(),
352 (*it)->makeLaTeXFile(writefile, mastertmpdir,
354 (*it)->markDepClean(mastertmpdir);
361 void BufferList::emergencyWriteAll()
363 for (BufferStorage::iterator it = bstore.begin();
364 it != bstore.end(); ++it) {
365 if (!(*it)->isLyxClean()) {
368 lyxerr <<_("lyx: Attempting to save"
371 << _(" as...") << endl;
373 for (int i = 0; i < 3 && !madeit; ++i) {
376 // We try to save three places:
377 // 1) Same place as document.
378 // 2) In HOME directory.
379 // 3) In "/tmp" directory.
381 s = (*it)->fileName();
383 s = AddName(GetEnvPath("HOME"),
385 } else { // MakeAbsPath to prepend the current drive letter on OS/2
386 s = AddName(MakeAbsPath("/tmp/"),
391 lyxerr << " " << i + 1 << ") " << s << endl;
393 if ((*it)->writeFile(s, true)) {
394 (*it)->markLyxClean();
395 lyxerr << _(" Save seems successful. "
399 lyxerr << _(" Save failed! Trying...")
402 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
410 Buffer * BufferList::readFile(string const & s, bool ronly)
412 Buffer * b = bstore.newBuffer(s, lyxrc, ronly);
415 string e = OnlyPath(s);
417 // File information about normal file
418 FileInfo fileInfo2(s);
420 // Check if emergency save file exists and is newer.
421 e += OnlyFilename(s) + ".emergency";
422 FileInfo fileInfoE(e);
424 bool use_emergency = false;
426 if (fileInfoE.exist() && fileInfo2.exist()) {
427 if (fileInfoE.getModificationTime()
428 > fileInfo2.getModificationTime()) {
429 if (AskQuestion(_("An emergency save of this document exists!"),
430 MakeDisplayPath(s, 50),
431 _("Try to load that instead?"))) {
433 // the file is not saved if we load the
436 use_emergency = true;
438 // Here, we should delete the emergency save
444 if (!use_emergency) {
445 // Now check if autosave file is newer.
447 a += OnlyFilename(s);
449 FileInfo fileInfoA(a);
450 if (fileInfoA.exist() && fileInfo2.exist()) {
451 if (fileInfoA.getModificationTime()
452 > fileInfo2.getModificationTime()) {
453 if (AskQuestion(_("Autosave file is newer."),
454 MakeDisplayPath(s, 50),
455 _("Load that one instead?"))) {
457 // the file is not saved if we load the
461 // Here, we should delete the autosave
467 // not sure if this is the correct place to begin LyXLex
470 if (b->readFile(lex))
479 bool BufferList::exists(string const & s)
481 for (BufferStorage::iterator it = bstore.begin();
482 it != bstore.end(); ++it) {
483 if ((*it)->fileName() == s)
490 Buffer * BufferList::getBuffer(string const & s)
492 for(BufferStorage::iterator it = bstore.begin();
493 it != bstore.end(); ++it) {
494 if ((*it)->fileName() == s)
501 Buffer * BufferList::newFile(string const & name, string tname)
503 /* get a free buffer */
504 Buffer * b = bstore.newBuffer(name, lyxrc);
506 // use defaults.lyx as a default template if it exists.
508 tname = LibFileSearch("templates", "defaults.lyx");
510 if (!tname.empty() && IsLyXFilename(tname)){
511 bool templateok = false;
515 if (b->readFile(lex)) {
520 WriteAlert(_("Error!"), _("Unable to open template"),
521 MakeDisplayPath(tname));
522 // no template, start with empty buffer
523 b->paragraph = new LyXParagraph;
526 else { // start with empty buffer
527 b->paragraph = new LyXParagraph;
531 b->setReadonly(false);
537 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
539 // make sure our path is absolute
540 string s = MakeAbsPath(filename);
542 // Is this done too early?
544 if (IsSGMLFilename(s)) {
546 if (fi.exist() && fi.readable()) {
547 if (!RunLinuxDoc(-1, s)) {
548 s = ChangeExtension (s, ".lyx", false);
549 } else { // sgml2lyx failed
550 WriteAlert(_("Error!"),
551 _("Could not convert file"), s);
555 // just change the extension and it will be
556 // handled like a regular lyx file that does
558 s = ChangeExtension(s, ".lyx", false);
562 // file already open?
564 if (AskQuestion(_("Document is already open:"),
565 MakeDisplayPath(s, 50),
566 _("Do you want to reload that document?"))) {
567 // Reload is accomplished by closing and then loading
568 if (!close(getBuffer(s))) {
571 // Fall through to new load. (Asger)
573 // Here, we pretend that we just loaded the
580 switch (IsFileWriteable(s)) {
582 minibuffer->Set(_("File `")+MakeDisplayPath(s, 50)+
583 _("' is read-only."));
589 b->lyxvc.file_found_hook(s);
591 break; //fine- it's r/w
593 // Here we probably should run
594 if (LyXVC::file_not_found_hook(s)) {
595 // Ask if the file should be checked out for
596 // viewing/editing, if so: load it.
597 lyxerr << "Do you want to checkout?" << endl;
599 if (AskQuestion(_("Cannot open specified file:"),
600 MakeDisplayPath(s, 50),
601 _("Create new document with this name?")))
603 // Find a free buffer
604 b = newFile(s, string());
609 if (b && tolastfiles)
610 lastfiles->newFile(b->fileName());