|
| 1 | +package fi.helsinki.cs.tmc.actions; |
| 2 | + |
| 3 | +import fi.helsinki.cs.tmc.core.utilities.SingletonTask; |
| 4 | +import fi.helsinki.cs.tmc.core.utilities.TmcRequestProcessor; |
| 5 | +import fi.helsinki.cs.tmc.coreimpl.TmcCoreSettingsImpl; |
| 6 | +import fi.helsinki.cs.tmc.ui.ConvenientDialogDisplayer; |
| 7 | +import fi.helsinki.cs.tmc.ui.TmcNotificationDisplayer; |
| 8 | +import fi.helsinki.cs.tmc.ui.TmcNotificationDisplayer.SingletonToken; |
| 9 | +import java.io.IOException; |
| 10 | +import java.nio.file.FileStore; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Paths; |
| 13 | +import java.text.DecimalFormat; |
| 14 | +import java.util.function.Consumer; |
| 15 | +import org.openide.awt.NotificationDisplayer; |
| 16 | +import org.openide.util.ImageUtilities; |
| 17 | + |
| 18 | +public class CheckDiskSpace { |
| 19 | + |
| 20 | + private static final long DEFAULT_CHECK_INTERVAL = 10 * 60 * 1000; |
| 21 | + |
| 22 | + private final TmcCoreSettingsImpl settings; |
| 23 | + private final SingletonToken notifierToken; |
| 24 | + |
| 25 | + public CheckDiskSpace(TmcCoreSettingsImpl settings) { |
| 26 | + this.settings = settings; |
| 27 | + this.notifierToken = TmcNotificationDisplayer.createSingletonToken(); |
| 28 | + } |
| 29 | + |
| 30 | + public void startCheckingPeriodically() { |
| 31 | + SingletonTask periodicChecker = new SingletonTask(() -> { |
| 32 | + doChecks((message) -> { |
| 33 | + TmcNotificationDisplayer.getDefault().notify(notifierToken, "You're running out of disk space", ImageUtilities.loadImageIcon("fi/helsinki/cs/tmc/ui/infobubble.png", false), message, null, NotificationDisplayer.Priority.HIGH); |
| 34 | + }); |
| 35 | + }, TmcRequestProcessor.instance); |
| 36 | + periodicChecker.setInterval(DEFAULT_CHECK_INTERVAL); |
| 37 | + } |
| 38 | + |
| 39 | + public void run() { |
| 40 | + doChecks((message) -> { |
| 41 | + ConvenientDialogDisplayer.getDefault().displayWarning(message); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + private void doChecks(Consumer<String> onMessage) { |
| 46 | + try { |
| 47 | + final FileStore fileStore = Files.getFileStore(settings.getConfigRoot()); |
| 48 | + // Gigabytes |
| 49 | + final double usableSpace = fileStore.getUsableSpace() / 1000000000.0; |
| 50 | + if (usableSpace < 1) { |
| 51 | + showDiskWarning(fileStore, usableSpace, onMessage); |
| 52 | + } else { |
| 53 | + final FileStore projectFileStore = Files.getFileStore(Paths.get(settings.getProjectRootDir())); |
| 54 | + final double projectUsableSpace = projectFileStore.getUsableSpace() / 1000000000.0; |
| 55 | + if (projectUsableSpace < 1) { |
| 56 | + showDiskWarning(projectFileStore, projectUsableSpace, onMessage); |
| 57 | + } |
| 58 | + } |
| 59 | + } catch (IOException ex) { |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private void showDiskWarning(FileStore store, double usableSpace, Consumer<String> notifier) { |
| 64 | + final String formattedSpaceAvailable = new DecimalFormat("##.##").format(usableSpace); |
| 65 | + final String fileStoreName = store.name(); |
| 66 | + final String message = "Your computer has only " + formattedSpaceAvailable + "GB of free disk space available.\n" |
| 67 | + + "Consider freeing up some space by deleting unused files or this program may not work properly!\n" |
| 68 | + + "\nThe lack of free space was detected in " + fileStoreName + "."; |
| 69 | + notifier.accept(message); |
| 70 | + } |
| 71 | +} |
0 commit comments