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(),
116 if (askMoreConfirmation &&
117 lyxrc->exit_confirmation &&
118 !AskQuestion(_("Some documents were not saved:"),
119 unsaved, _("Exit anyway?"))) {
127 // Should probably be moved to somewhere else: BufferView? LyXView?
128 bool BufferList::write(Buffer * buf, bool makeBackup)
130 minibuffer->Set(_("Saving document"),
131 MakeDisplayPath(buf->fileName()), "...");
133 // We don't need autosaves in the immediate future. (Asger)
134 buf->resetAutosaveTimers();
138 string s = buf->fileName() + '~';
139 // Rename is the wrong way of making a backup,
140 // this is the correct way.
141 /* truss cp fil fil2:
142 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
143 stat("LyXVC.lyx", 0xEFFFF688) = 0
144 open("LyXVC.lyx", O_RDONLY) = 3
145 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
146 fstat(4, 0xEFFFF508) = 0
147 fstat(3, 0xEFFFF508) = 0
148 read(3, " # T h i s f i l e w".., 8192) = 5579
149 write(4, " # T h i s f i l e w".., 5579) = 5579
150 read(3, 0xEFFFD4A0, 8192) = 0
153 chmod("LyXVC3.lyx", 0100644) = 0
154 lseek(0, 0, SEEK_CUR) = 46440
158 // Should proabaly have some more error checking here.
159 // Should be cleaned up in 0.13, at least a bit.
160 // Doing it this way, also makes the inodes stay the same.
161 // This is still not a very good solution, in particular we
162 // might loose the owner of the backup.
163 FileInfo finfo(buf->fileName());
165 mode_t fmode = finfo.getMode();
166 struct utimbuf * times = new struct utimbuf;
168 times->actime = finfo.getAccessTime();
169 times->modtime = finfo.getModificationTime();
170 long blksize = finfo.getBlockSize();
171 lyxerr.debug() << "BlockSize: " << blksize << endl;
172 FilePtr fin(buf->fileName(), FilePtr::read);
173 FilePtr fout(s, FilePtr::truncate);
174 if (fin() && fout()) {
175 char * cbuf = new char[blksize+1];
179 c_read = fread(cbuf, 1, blksize, fin);
187 chmod(s.c_str(), fmode);
189 if (utime(s.c_str(), times)) {
190 lyxerr << "utime error." << endl;
194 lyxerr << "LyX was not able to make "
195 "backupcopy. Beware." << endl;
201 if (buf->writeFile(buf->fileName(), false)) {
204 minibuffer->Set(_("Document saved as"),
205 MakeDisplayPath(buf->fileName()));
207 // now delete the autosavefile
208 string a = OnlyPath(buf->fileName());
210 a += OnlyFilename(buf->fileName());
212 FileInfo fileinfo(a);
213 if (fileinfo.exist()) {
214 if (remove(a.c_str()) != 0) {
215 WriteFSAlert(_("Could not delete "
216 "auto-save file!"), a);
220 // Saving failed, so backup is not backup
222 string s = buf->fileName() + '~';
223 rename(s.c_str(), buf->fileName().c_str());
225 minibuffer->Set(_("Save failed!"));
233 void BufferList::closeAll()
235 _state = BufferList::CLOSING;
236 while (!bstore.empty()) {
237 close(bstore.front());
239 _state = BufferList::OK;
243 void BufferList::resize()
245 for(BufferStorage::iterator it = bstore.begin();
246 it != bstore.end(); ++it) {
252 bool BufferList::close(Buffer * buf)
256 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
258 switch(AskConfirmation(_("Changes in document:"),
259 MakeDisplayPath(buf->fileName(), 50),
260 _("Save document?"))){
263 lastfiles->newFile(buf->fileName());
281 void BufferList::makePup(int pup)
282 /* This should be changed to return a char const *const
283 in the same way as for lastfiles.[hC]
287 for(BufferStorage::iterator it = bstore.begin();
288 it != bstore.end(); ++it) {
289 string relbuf = MakeDisplayPath((*it)->fileName(), 30);
290 fl_addtopup(pup, relbuf.c_str());
293 if (ant == 0) fl_addtopup(pup, _("No Documents Open!%t"));
297 Buffer * BufferList::first()
299 if (bstore.empty()) return 0;
300 return bstore.front();
304 Buffer * BufferList::getBuffer(int choice)
306 if (choice >= bstore.size()) return 0;
307 return bstore[choice];
311 void BufferList::updateInset(Inset * inset, bool mark_dirty)
313 for (BufferStorage::iterator it = bstore.begin();
314 it != bstore.end(); ++it) {
316 && (*it)->getUser()->text->UpdateInset(inset)) {
325 int BufferList::unlockInset(UpdatableInset * inset)
327 if (!inset) return 1;
328 for(BufferStorage::iterator it = bstore.begin();
329 it != bstore.end(); ++it) {
330 if ((*it)->the_locking_inset == inset) {
331 (*it)->InsetUnlock();
339 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
341 for(BufferStorage::iterator it = bstore.begin();
342 it != bstore.end(); ++it) {
343 if (!(*it)->isDepClean(mastertmpdir)) {
344 string writefile = mastertmpdir;
346 writefile += ChangeExtension((*it)->fileName(),
348 (*it)->makeLaTeXFile(writefile, mastertmpdir,
350 (*it)->markDepClean(mastertmpdir);
356 void BufferList::emergencyWriteAll()
358 for (BufferStorage::iterator it = bstore.begin();
359 it != bstore.end(); ++it) {
360 if (!(*it)->isLyxClean()) {
363 lyxerr <<_("lyx: Attempting to save"
366 << _(" as...") << endl;
368 for (int i = 0; i < 3 && !madeit; ++i) {
371 // We try to save three places:
372 // 1) Same place as document.
373 // 2) In HOME directory.
374 // 3) In "/tmp" directory.
376 s = (*it)->fileName();
378 s = AddName(GetEnvPath("HOME"),
381 // MakeAbsPath to prepend the current
382 // drive letter on OS/2
383 s = AddName(MakeAbsPath("/tmp/"),
388 lyxerr << " " << i + 1 << ") " << s << endl;
390 if ((*it)->writeFile(s, true)) {
391 (*it)->markLyxClean();
392 lyxerr << _(" Save seems successful. "
396 lyxerr << _(" Save failed! Trying...")
399 lyxerr << _(" Save failed! Bummer. "
409 Buffer * BufferList::readFile(string const & s, bool ronly)
411 Buffer * b = bstore.newBuffer(s, lyxrc, ronly);
414 string e = OnlyPath(s);
416 // File information about normal file
417 FileInfo fileInfo2(s);
419 // Check if emergency save file exists and is newer.
420 e += OnlyFilename(s) + ".emergency";
421 FileInfo fileInfoE(e);
423 bool use_emergency = false;
425 if (fileInfoE.exist() && fileInfo2.exist()) {
426 if (fileInfoE.getModificationTime()
427 > fileInfo2.getModificationTime()) {
428 if (AskQuestion(_("An emergency save of this document exists!"),
429 MakeDisplayPath(s, 50),
430 _("Try to load that instead?"))) {
432 // the file is not saved if we load the
435 use_emergency = true;
437 // Here, we should delete the emergency save
443 if (!use_emergency) {
444 // Now check if autosave file is newer.
446 a += OnlyFilename(s);
448 FileInfo fileInfoA(a);
449 if (fileInfoA.exist() && fileInfo2.exist()) {
450 if (fileInfoA.getModificationTime()
451 > fileInfo2.getModificationTime()) {
452 if (AskQuestion(_("Autosave file is newer."),
453 MakeDisplayPath(s, 50),
454 _("Load that one instead?"))) {
456 // the file is not saved if we load the
460 // Here, we should delete the autosave
466 // not sure if this is the correct place to begin LyXLex
469 if (b->readFile(lex))
478 bool BufferList::exists(string const & s)
480 for (BufferStorage::iterator it = bstore.begin();
481 it != bstore.end(); ++it) {
482 if ((*it)->fileName() == s)
489 Buffer * BufferList::getBuffer(string const & s)
491 for(BufferStorage::iterator it = bstore.begin();
492 it != bstore.end(); ++it) {
493 if ((*it)->fileName() == s)
500 Buffer * BufferList::newFile(string const & name, string tname)
502 /* get a free buffer */
503 Buffer * b = bstore.newBuffer(name, lyxrc);
505 // use defaults.lyx as a default template if it exists.
507 tname = LibFileSearch("templates", "defaults.lyx");
509 if (!tname.empty() && IsLyXFilename(tname)){
510 bool templateok = false;
514 if (b->readFile(lex)) {
519 WriteAlert(_("Error!"), _("Unable to open template"),
520 MakeDisplayPath(tname));
521 // no template, start with empty buffer
522 b->paragraph = new LyXParagraph;
525 else { // start with empty buffer
526 b->paragraph = new LyXParagraph;
530 b->setReadonly(false);
536 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
538 // make sure our path is absolute
539 string s = MakeAbsPath(filename);
541 // Is this done too early?
543 if (IsSGMLFilename(s)) {
545 if (fi.exist() && fi.readable()) {
546 if (!RunLinuxDoc(-1, s)) {
547 s = ChangeExtension (s, ".lyx", false);
548 } else { // sgml2lyx failed
549 WriteAlert(_("Error!"),
550 _("Could not convert file"), s);
554 // just change the extension and it will be
555 // handled like a regular lyx file that does
557 s = ChangeExtension(s, ".lyx", false);
561 // file already open?
563 if (AskQuestion(_("Document is already open:"),
564 MakeDisplayPath(s, 50),
565 _("Do you want to reload that document?"))) {
566 // Reload is accomplished by closing and then loading
567 if (!close(getBuffer(s))) {
570 // Fall through to new load. (Asger)
572 // Here, we pretend that we just loaded the
579 switch (IsFileWriteable(s)) {
581 minibuffer->Set(_("File `")+MakeDisplayPath(s, 50)+
582 _("' is read-only."));
588 b->lyxvc.file_found_hook(s);
590 break; //fine- it's r/w
592 // Here we probably should run
593 if (LyXVC::file_not_found_hook(s)) {
594 // Ask if the file should be checked out for
595 // viewing/editing, if so: load it.
596 lyxerr << "Do you want to checkout?" << endl;
598 if (AskQuestion(_("Cannot open specified file:"),
599 MakeDisplayPath(s, 50),
600 _("Create new document with this name?")))
602 // Find a free buffer
603 b = newFile(s, string());
608 if (b && tolastfiles)
609 lastfiles->newFile(b->fileName());