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
26 #include <sys/types.h>
28 #include "bufferlist.h"
30 #include "minibuffer.h"
31 #include "support/FileInfo.h"
32 #include "support/filetools.h"
33 #include "lyx_gui_misc.h"
34 #include "lastfiles.h"
37 #include "lyxscreen.h"
43 extern BufferView * current_view;
44 extern void BeforeChange();
45 extern int RunLinuxDoc(int, string const &);
48 // Class BufferStorage
51 void BufferStorage::release(Buffer * buf)
53 for(Container::iterator it = container.begin();
54 it != container.end(); ++it) {
56 Buffer * tmpbuf = (*it);
65 Buffer * BufferStorage::newBuffer(string const & s,
69 Buffer * tmpbuf = new Buffer(s, lyxrc, ronly);
70 tmpbuf->params.useClassDefaults();
71 lyxerr.debug() << "Assigning to buffer "
72 << container.size() + 1 << endl;
73 container.push_back(tmpbuf);
82 BufferList::BufferList()
84 _state = BufferList::OK;
88 bool BufferList::empty()
90 return bstore.empty();
94 extern void MenuWrite(Buffer *);
96 bool BufferList::QwriteAll()
98 bool askMoreConfirmation = false;
100 for(BufferStorage::iterator it = bstore.begin();
101 it != bstore.end(); ++it) {
102 if (!(*it)->isLyxClean()) {
103 switch(AskConfirmation(_("Changes in document:"),
104 MakeDisplayPath((*it)->fileName(),
106 _("Save document?"))) {
111 askMoreConfirmation = true;
112 unsaved += MakeDisplayPath((*it)->fileName(),
121 if (askMoreConfirmation &&
122 lyxrc->exit_confirmation &&
123 !AskQuestion(_("Some documents were not saved:"),
124 unsaved, _("Exit anyway?"))) {
132 // Should probably be moved to somewhere else: BufferView? LyXView?
133 bool BufferList::write(Buffer * buf, bool makeBackup)
135 current_view->owner()->getMiniBuffer()->Set(_("Saving document"),
136 MakeDisplayPath(buf->fileName()), "...");
138 // We don't need autosaves in the immediate future. (Asger)
139 buf->resetAutosaveTimers();
143 string s = buf->fileName() + '~';
144 // Rename is the wrong way of making a backup,
145 // this is the correct way.
146 /* truss cp fil fil2:
147 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
148 stat("LyXVC.lyx", 0xEFFFF688) = 0
149 open("LyXVC.lyx", O_RDONLY) = 3
150 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
151 fstat(4, 0xEFFFF508) = 0
152 fstat(3, 0xEFFFF508) = 0
153 read(3, " # T h i s f i l e w".., 8192) = 5579
154 write(4, " # T h i s f i l e w".., 5579) = 5579
155 read(3, 0xEFFFD4A0, 8192) = 0
158 chmod("LyXVC3.lyx", 0100644) = 0
159 lseek(0, 0, SEEK_CUR) = 46440
163 // Should proabaly have some more error checking here.
164 // Should be cleaned up in 0.13, at least a bit.
165 // Doing it this way, also makes the inodes stay the same.
166 // This is still not a very good solution, in particular we
167 // might loose the owner of the backup.
168 FileInfo finfo(buf->fileName());
170 mode_t fmode = finfo.getMode();
171 struct utimbuf * times = new struct utimbuf;
173 times->actime = finfo.getAccessTime();
174 times->modtime = finfo.getModificationTime();
175 long blksize = finfo.getBlockSize();
176 lyxerr.debug() << "BlockSize: " << blksize << endl;
177 ifstream ifs(buf->fileName().c_str());
178 ofstream ofs(s.c_str(), ios::out|ios::trunc);
186 chmod(s.c_str(), fmode);
188 if (utime(s.c_str(), times)) {
189 lyxerr << "utime error." << endl;
192 lyxerr << "LyX was not able to make "
193 "backupcopy. Beware." << endl;
199 if (buf->writeFile(buf->fileName(), false)) {
202 current_view->owner()->getMiniBuffer()->
203 Set(_("Document saved as"),
204 MakeDisplayPath(buf->fileName()));
206 // now delete the autosavefile
207 string a = OnlyPath(buf->fileName());
209 a += OnlyFilename(buf->fileName());
211 FileInfo fileinfo(a);
212 if (fileinfo.exist()) {
213 if (remove(a.c_str()) != 0) {
214 WriteFSAlert(_("Could not delete "
215 "auto-save file!"), a);
219 // Saving failed, so backup is not backup
221 string s = buf->fileName() + '~';
222 rename(s.c_str(), buf->fileName().c_str());
224 current_view->owner()->getMiniBuffer()->Set(_("Save failed!"));
232 void BufferList::closeAll()
234 _state = BufferList::CLOSING;
235 while (!bstore.empty()) {
236 close(bstore.front());
238 _state = BufferList::OK;
242 void BufferList::resize()
244 for(BufferStorage::iterator it = bstore.begin();
245 it != bstore.end(); ++it) {
251 bool BufferList::close(Buffer * buf)
255 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
257 switch(AskConfirmation(_("Changes in document:"),
258 MakeDisplayPath(buf->fileName(), 50),
259 _("Save document?"))){
262 lastfiles->newFile(buf->fileName());
280 void BufferList::makePup(int pup)
281 /* This should be changed to return a char const *const
282 in the same way as for lastfiles.[hC]
286 for(BufferStorage::iterator it = bstore.begin();
287 it != bstore.end(); ++it) {
288 string relbuf = MakeDisplayPath((*it)->fileName(), 30);
289 fl_addtopup(pup, relbuf.c_str());
292 if (ant == 0) fl_addtopup(pup, _("No Documents Open!%t"));
296 Buffer * BufferList::first()
298 if (bstore.empty()) return 0;
299 return bstore.front();
303 Buffer * BufferList::getBuffer(int choice)
305 if (choice >= bstore.size()) return 0;
306 return bstore[choice];
310 void BufferList::updateInset(Inset * inset, bool mark_dirty)
312 for (BufferStorage::iterator it = bstore.begin();
313 it != bstore.end(); ++it) {
315 && (*it)->getUser()->text->UpdateInset(inset)) {
324 int BufferList::unlockInset(UpdatableInset * inset)
326 if (!inset) return 1;
327 for(BufferStorage::iterator it = bstore.begin();
328 it != bstore.end(); ++it) {
329 if ((*it)->the_locking_inset == inset) {
330 (*it)->InsetUnlock();
338 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
340 for(BufferStorage::iterator it = bstore.begin();
341 it != bstore.end(); ++it) {
342 if (!(*it)->isDepClean(mastertmpdir)) {
343 string writefile = mastertmpdir;
345 writefile += ChangeExtension((*it)->fileName(),
347 (*it)->makeLaTeXFile(writefile, mastertmpdir,
349 (*it)->markDepClean(mastertmpdir);
355 void BufferList::emergencyWriteAll()
357 for (BufferStorage::iterator it = bstore.begin();
358 it != bstore.end(); ++it) {
359 if (!(*it)->isLyxClean()) {
362 lyxerr <<_("lyx: Attempting to save"
365 << _(" as...") << endl;
367 for (int i = 0; i < 3 && !madeit; ++i) {
370 // We try to save three places:
371 // 1) Same place as document.
372 // 2) In HOME directory.
373 // 3) In "/tmp" directory.
375 s = (*it)->fileName();
377 s = AddName(GetEnvPath("HOME"),
380 // MakeAbsPath to prepend the current
381 // drive letter on OS/2
382 s = AddName(MakeAbsPath("/tmp/"),
387 lyxerr << " " << i + 1 << ") " << s << endl;
389 if ((*it)->writeFile(s, true)) {
390 (*it)->markLyxClean();
391 lyxerr << _(" Save seems successful. "
395 lyxerr << _(" Save failed! Trying...")
398 lyxerr << _(" Save failed! Bummer. "
408 Buffer * BufferList::readFile(string const & s, bool ronly)
410 Buffer * b = bstore.newBuffer(s, lyxrc, ronly);
413 string e = OnlyPath(s);
415 // File information about normal file
416 FileInfo fileInfo2(s);
418 // Check if emergency save file exists and is newer.
419 e += OnlyFilename(s) + ".emergency";
420 FileInfo fileInfoE(e);
422 bool use_emergency = false;
424 if (fileInfoE.exist() && fileInfo2.exist()) {
425 if (fileInfoE.getModificationTime()
426 > fileInfo2.getModificationTime()) {
427 if (AskQuestion(_("An emergency save of this document exists!"),
428 MakeDisplayPath(s, 50),
429 _("Try to load that instead?"))) {
431 // the file is not saved if we load the
434 use_emergency = true;
436 // Here, we should delete the emergency save
442 if (!use_emergency) {
443 // Now check if autosave file is newer.
445 a += OnlyFilename(s);
447 FileInfo fileInfoA(a);
448 if (fileInfoA.exist() && fileInfo2.exist()) {
449 if (fileInfoA.getModificationTime()
450 > fileInfo2.getModificationTime()) {
451 if (AskQuestion(_("Autosave file is newer."),
452 MakeDisplayPath(s, 50),
453 _("Load that one instead?"))) {
455 // the file is not saved if we load the
459 // Here, we should delete the autosave
465 // not sure if this is the correct place to begin LyXLex
468 if (b->readFile(lex))
477 bool BufferList::exists(string const & s)
479 for (BufferStorage::iterator it = bstore.begin();
480 it != bstore.end(); ++it) {
481 if ((*it)->fileName() == s)
488 Buffer * BufferList::getBuffer(string const & s)
490 for(BufferStorage::iterator it = bstore.begin();
491 it != bstore.end(); ++it) {
492 if ((*it)->fileName() == s)
499 Buffer * BufferList::newFile(string const & name, string tname)
501 /* get a free buffer */
502 Buffer * b = bstore.newBuffer(name, lyxrc);
504 // use defaults.lyx as a default template if it exists.
506 tname = LibFileSearch("templates", "defaults.lyx");
508 if (!tname.empty() && IsLyXFilename(tname)){
509 bool templateok = false;
513 if (b->readFile(lex)) {
518 WriteAlert(_("Error!"), _("Unable to open template"),
519 MakeDisplayPath(tname));
520 // no template, start with empty buffer
521 b->paragraph = new LyXParagraph;
524 else { // start with empty buffer
525 b->paragraph = new LyXParagraph;
529 b->setReadonly(false);
535 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
537 // make sure our path is absolute
538 string s = MakeAbsPath(filename);
540 // Is this done too early?
542 if (IsSGMLFilename(s)) {
544 if (fi.exist() && fi.readable()) {
545 if (!RunLinuxDoc(-1, s)) {
546 s = ChangeExtension (s, ".lyx", false);
547 } else { // sgml2lyx failed
548 WriteAlert(_("Error!"),
549 _("Could not convert file"), s);
553 // just change the extension and it will be
554 // handled like a regular lyx file that does
556 s = ChangeExtension(s, ".lyx", false);
560 // file already open?
562 if (AskQuestion(_("Document is already open:"),
563 MakeDisplayPath(s, 50),
564 _("Do you want to reload that document?"))) {
565 // Reload is accomplished by closing and then loading
566 if (!close(getBuffer(s))) {
569 // Fall through to new load. (Asger)
571 // Here, we pretend that we just loaded the
578 switch (IsFileWriteable(s)) {
580 current_view->owner()->getMiniBuffer()->
581 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());